File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services and the fly likes default namespaces are being dropped when sent through a web service Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "default namespaces are being dropped when sent through a web service" Watch "default namespaces are being dropped when sent through a web service" New topic
Author

default namespaces are being dropped when sent through a web service

Andrew Carney
Ranch Hand

Joined: Oct 17, 2006
Posts: 96
Hello,

I am sending the root node of an xml over to a web service in this way:
1. I create the xml as a long Java String.
2. I convert the xml to a Document object and get the root node using this code:



The problem is that the other party who receives the root node complains that the default namespaces (xmlns attributes) are being dropped from the xml that he's receiving although I do include it in the xml String.
Any idea what could cause that?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Sorry to tell you this: it's your code which is causing it.

By default a DOM parser is not namespace-aware. (Yes, it's possible to parse XML while discarding namespace information.) There's a setNamespaceAware() method on DocumentBuilderFactory -- or possibly on DocumentBuilder, I don't quite remember -- so use that.
Andrew Carney
Ranch Hand

Joined: Oct 17, 2006
Posts: 96

Thank you for bringing this to my attention Paul.
You are correct, the default factory has namespaceaware set to false, I thought it is true by default.
I have added the setNamespaceAware(true) but it still seems to drop default namespaces, so for example:

This one goes through: xmlns:xsi="http://www.w3.org/2001/04/xmlenc#"
But this one doesn't: xmlns="http://www.w3.org/2001/04/xmlenc#"




Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Roy Cohen wrote:
Thank you for bringing this to my attention Paul.
You are correct, the default factory has namespaceaware set to false, I thought it is true by default.

Well, it really should be true by default, but the API writers are stuck with the decision they made when they first designed those classes.
I have added the setNamespaceAware(true) but it still seems to drop default namespaces, so for example:

This one goes through: xmlns:xsi="http://www.w3.org/2001/04/xmlenc#"
But this one doesn't: xmlns="http://www.w3.org/2001/04/xmlenc#"


Then you must be doing something else wrong. Presumably you're manipulating that DOM in some way, otherwise you could just send the XML directly to the web service without producing a DOM. And perhaps that manipulation includes creating nodes which are in no namespace rather than in the default namespace. But anyway that's just guessing, you should really explain what you are doing.
Andrew Carney
Ranch Hand

Joined: Oct 17, 2006
Posts: 96
The reason why I am transforming it to DOM is because the web service expects object type Node which is the root node of the xml and since I am creating the xml as a long String I need to transform it afterwards. I am aware that there is an API that allows you to create XMLs "properly" however in this case it is much simpler creating it as a String.
The xml does have nodes with no namespaces but I must follow the xml the other side expects and I am except that the defaul namespaces seems to be droppping on transformation...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Sorry, it's pretty hard to look for problems when we can't see anything.
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
I don't know if this is of any help to you, but the SAAJ Node class is a subclass of the DOM Node class, so you can use SAAJ to create a SOAP message, which can later be manipulated by DOM.
A word of warning: If DOM, then SAAJ and finally DOM again is used to manipulate a SOAP message, references to DOM nodes can become invalid.
Best wishes!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: default namespaces are being dropped when sent through a web service
 
Similar Threads
XPathAPI and namespaces
Validating XML with XSD..problems
Appending part XML to main XML
how can i add namespaces to a xml document
How to get rid of this exception?