• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

XmlSlurper and Namespaces

 
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Marshal
Posts: 74054
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done sorting it out, and giving the solution
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 26783
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Filed a bugreport:
http://jira.codehaus.org/browse/GROOVY-4637
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Came across this page trying to overcome the same problem today. I have complex xml from which I need to parse values. There are twelve namespaces. What is the best practice today ten years after this post to do this in a groovy script? Can anyone offer an example?
Thanks in advance for any help.
 
Paul Clapham
Sheriff
Posts: 26783
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That post was 10 years ago, I would expect the bug is fixed by now. Is it not?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic