I have this code:
#!/usr/bin/groovy
import javax.xml.xpath.*
import javax.xml.parsers.DocumentBuilderFactory
def testxml = '''
<Employee>
<ID>..</ID>
<E-mail>..</E-mail>
<custom_1>foo</custom_1>
<custom_2>bar</custom_2>
<custom_3>base</custom_3>
</Employee>
'''
def processXml( String xml, String xpathQuery ) {
def xpath = XPathFactory.newInstance().newXPath()
def builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
def inputStream = new ByteArrayInputStream( xml.bytes )
def records = builder.parse(inputStream).documentElement
xpath.evaluate( xpathQuery, records )
}
println processXml( testxml, '//*[starts-with(name(), "custom")]' )
and instead of returning all the nodes (I provided //
in Xpath expression), I get only the first node. How can I modify my code to display all the matching nodes ?
according to the docs http://docs.oracle.com/javase/7/docs/api/javax/xml/xpath/package-summary.html you pass
evaluate
what you want, which defaults to string. So request a NODESET:and iterate over the resulting
NodeList
: