i have checked around the site and although many people are looking for similar solutions, a solution to my specific problem has not been posted, nor has a similar question been asked.
i have an XML that looks like this:
<root>
<item1>TEXT A</item1>
<item2>TEXT B</item2>
<subs>
<sub1>
<thing property1='valueA' property2='valueB'>
<foo>fooA</foo>
</sub1>
<sub2>
<thing property1='valueC' property2='valueD'>
<foo>fooA</foo>
</sub2>
</subs>
</root>
i want to get "TEXT B"
i am thinking
def xml = new XmlParser().parseText(MY_XML_FILE)
but now how do i access "TEXT B" ??
xml.root.item2.text
does not work. any thoughts?
There's a few things going on here:
Groovy's XmlParser and XmlSlurper (I recommend the latter) take an XML document wrapped in a root element (well-formed XML always exists in a root element). Groovy doesn't require you to specify the root element, so
xml.root.item1
can become
xml.item1
If you have many item1s, you need to access them as an array.
xml.item1[0]
Your XML is not well-formed. You're missing two end tags.
To get the inner text of a node, use text() instead of text (it's a method).
Here are some excellent resources on both XmlParser and XmlSlurper .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.