Looking for value in an xpath - groovy

0

Very good, I currently have the following xml file (person.xml)

          Jane       Smith       31          Juan     Smith     31 In the process that I am doing, I know what the value of var will be and what I need to obtain is the value of "firstName"

The first query I was preparing was:

def person = new XmlParser().parse(new File("/tmp/person.xml"))
println "first name: ${people.person[0].firstName.text()}"

The funny thing that people do not recognize it groovy.lang.MissingPropertyException: No such property:

If both in the xml remove the tag people, the previous query works.

If I try to launch the query

println "first name: ${person.@var=='hola'.firstName.text()}"

I see the same error above groovy.lang.MissingPropertyException: No such property:

How should I launch the query so that given the value of "var" I can get the value of firstName?

greetings Javi

    
asked by Javi Gomez 04.06.2018 в 16:48
source

1 answer

0

Very good, This is what I did and it is operational:

import groovy.util.XmlParser
import groovy.util.XmlSlurper
import groovy.util.slurpersupport.GPathResult

def String xml = """<people>
                        <person var="1">
                            <firstName>Jane</firstName>
                            <lastName>Smith</lastName>
                            <age>31</age>
                        </person>
                        <person var="2">
                            <firstName>Juan</firstName>
                            <lastName>Smith</lastName>
                            <age>32</age>
                        </person>
                    </people>
                  """
def GPathResult people = new XmlSlurper().parseText(xml)
def GPathResult person = people.children().find { GPathResult node ->
    node.name() == 'person' && node.getProperty("@var") == '2'
}

def GPathResult firstName = person.children().find { GPathResult node ->
    node.name() == 'firstName'
}
firstName.text()
    
answered by 05.06.2018 в 10:34