Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.xpath.*

xpath = '''
  /doc/person[@name="alan"]/g
  [.=/doc/person[@name="paul"]/g]
  [.=/doc/person[@name="ivan"]/g]
'''

builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
doc     = builder.parse(new ByteArrayInputStream(xml.bytes))
expr    = XPathFactory.newInstance().newXPath().compile(xpath)
nodes   = expr.evaluate(doc, XPathConstants.NODESET)
assert    nodes.collect { node -> node.textContent } == ['2', '4']

...