IntelliJ Java IDE
The moose likes Groovy and the fly likes XmlSlurper and Namespaces Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Other Languages » Groovy
Reply Bookmark "XmlSlurper and Namespaces" Watch "XmlSlurper and Namespaces" New topic
Author

XmlSlurper and Namespaces

Ivan Krizsan
Bartender

Joined: Oct 04, 2006
Posts: 2186
Hi!
I have a XML document which contains an element with two attributes with the same name, but belonging to different namespaces:

If I configure an XMLSlurper to be namespace aware and supplies a map with the two namespaces ("":"http://somenamespace" and "":"http://someothernamespace").
When I later try to access the contents of ItemId that belongs to the ns2 namespace using the following expression:

I always get the result "Number1".
What am I doing wrong?
Thanks in advance!


My free books and tutorials: http://www.slideshare.net/krizsan
Ivan Krizsan
Bartender

Joined: Oct 04, 2006
Posts: 2186
Hi!
Answering my own question:
It seems like XmlSlurper and GPathResults is not able to handle the case described above, instead I turned to XmlParser.
Here is a complete example program showing the error as well as the solution:

There output from the above is:

Attribute value: attributeData1
Attribute value: attributeData2

Best wishes!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 26710
Well done sorting it out, and giving the solution
Ivan Krizsan
Bartender

Joined: Oct 04, 2006
Posts: 2186
Hi!
I have been looking further into this and found that XmlSlurper disregards namespaces when storing attributes of an element, using only the local name of the attribute.
This will cause an attribute with the same name as a previous attribute to overwrite the earlier attribute (with the same name).
I have also devised a fix for this which I am going to submit to the Groovy developers for examination.
To me, this looks like a bug.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 13842

Looks like a bug to me.

Just one thing: XML attribute names can't be in the default namespace, unlike element names, which can. I see you have a default namespace in your examples. That might be part of the problem for the Groovy implementation but nevertheless the output you're seeing definitely looks wrong to me.
Ivan Krizsan
Bartender

Joined: Oct 04, 2006
Posts: 2186
Filed a bugreport:
http://jira.codehaus.org/browse/GROOVY-4637
 
 
subject: XmlSlurper and Namespaces
 
Threads others viewed
Web Service can't read my SOAP
random xmlns attribute order causing unexpected XML tag exception
need to add namespace ns2: in the all webservices response tags.
differnce between -p and -ns2p options while using wsdl2java
Namespace prefix used but not declared
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture