I have an xml node that i want to check for a specific string for example <name.firstname>john</name.firstname> therefore i want to run through the whole xml file and find all firstname and then get the value which is john can anyone help me with this
-
2and what have you tried so far?cfrick– cfrick2015-07-04 21:54:39 +00:00Commented Jul 4, 2015 at 21:54
-
Did you check xml primer in groovy: groovy-lang.org/processing-xml.htmlJayan– Jayan2015-07-05 15:20:08 +00:00Commented Jul 5, 2015 at 15:20
Add a comment
|
1 Answer
Suppose the XML was formatted like this:
<results>
<person>
<name.firstname>john</name.firstname>
</person>
<person>
<name.firstname>mary</name.firstname>
</person>
</results>
With Groovy and XmlSlurper you can parse out the first name element with following code:
def results = new XmlSlurper().parseText(text)
def people = results.person // list of person elements of type NodeChildren
people.each{ person ->
String name = person."name.firstname".text()
println name
}
You need to quote the "name.firstname" element since the "." would be confused with the XPath notation.
1 Comment
john
Actually I did that but for what im currently doing that is not efficient I am using DOMcategory for parsing the trick is to search firstname and overlook the rest