Search code examples
groovyxmlslurper

Unable to find content by tag name using XMLSlurper


I'm trying to search in an XML file using XMLSlurper and after reading some past questions thought this would be the way to do so, however it isn't working.

This is the xml content:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="Permissions_DontShowAgain">Do not show again</string>
    <string name="Version">1.32.3</string>
    <string name="RetryNow">Retry Now</string>
    ...

And this is the code:

def result = new XmlSlurper().parse(file)
assert result instanceof groovy.util.slurpersupport.GPathResult
def version = result.depthFirst().findAll { it.name() == 'Version' }
println version

If I do a println after the slurping I can confirm the file has been found and the result variable contains its content. But if I print out version its []. I want to obtain the 1.32.3 value.


Solution

  • You want the attribute name, not the tag name

    result.depthFirst().findAll { [email protected]() == 'Version' }