I've been struggling for a long time with XmlSlurper in how I can read
attributes. Unfortunately, I have an XML document with namespaces. I wish I
could ignore the namespaces, but while XmlSlurper no-arg constructor says it
ignores namespaces, it does not. When using two-arg constructor to set
namespaceAware to false it just asks like namespaced elements are deleted. So
I'm trying to figure out how to specify namespaces:
import groovy.xml.*
def text = """<x:root xmlns:x="blah">
<x:child x:id='1'>c</x:child>
</x:root>"""
def xml =
new XmlSlurper()
.parseText(text)
.declareNamespace(x:'blah')
// .declareNamespace(t:'blah')
println xml.child.text() //"c" always
println xml.'x:child'.text() //"c" when declareNamespace x
println xml.'t:child'.text() //"c" when declareNamespace t
println xml.child.'@x:id' //"1" always
println xml.child.'@t:id' //"" always
It appears that specifying namespace is optional on elements and also
declareNamespace affects how I find the elements when they do have namespaces.
For attributes, declareNamespace appears to have no effect, and I need to
specify the prefix as it is specified in the file itself. The problem is that
the generator gets to specify any prefix they want. How can I get the "id"
attribute on the "child" element regardless of the namespace prefix used? (A
solution dropping all namespaces is fine as there is only one namespace and no
collisions).
Thanks,
Jason Winnebeck
----------------------------------------------------------------------
This email message and any attachments are for the sole use of the intended
recipient(s). Any unauthorized review, use, disclosure or distribution is
prohibited. If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message and any attachments.