0

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

2
  • 2
    and what have you tried so far? Commented Jul 4, 2015 at 21:54
  • Did you check xml primer in groovy: groovy-lang.org/processing-xml.html Commented Jul 5, 2015 at 15:20

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.