我有这个代码:
#!/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")]' )
而不是返回所有节点(我//
在 Xpath 表达式中提供),我只得到第一个节点。如何修改我的代码以显示所有匹配的节点?