Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

default namespaces are being dropped when sent through a web service

 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, it's pretty hard to look for problems when we can't see anything.
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic