The situation:
def str = """
<foo xmlns:weird="http://localhost/">
<bar>sudo </bar>
<weird:bar>make me a sandwich!</weird:bar>
</foo>
"""
def xml = new XmlSlurper().parseText(str)
println xml.bar
The output of this snippet is
# sudo make me a sandwich!
It seems like the parser merges the contents of <bar>
and <weird:bar>
.
Is this behavior desired and if yes, how can I avoid this and select only <bar>
or <weird:bar>
?
By default XMLSlurper is not namespace aware. This can be turned on by declaring namespaces with the
declareNamespace
Method.The output is:
The first
println
will still not be namespace aware. The secondprintln
will only print the tag without namespace. If you qualify element with the prefix shown in the thirdprintln
you only get the namespaced tag.I know this was answered a while ago, but here's an alternative for anyone else facing the same issue. The
XmlSlurper
class has three constructors, a couple of which allow you to specify you want it to be namespace-aware.Declare the slurper by calling
new XmlSlurper(false, true)
. I hope this is useful to others.