Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find element by attribute value in GPath?

Tags:

What is an alternative to this XPath //div[@id='foo'] in GPath? In general, where I can find this documentation?

like image 586
yegor256 Avatar asked Aug 11 '11 04:08

yegor256


2 Answers

Here is the corresponding snippet:

def node = new XmlSlurper().parseText(...) def foo = node.depthFirst().findAll { it.name() == 'div' && it.@id == 'foo'} 

A few other links you may want to read:

  • GPath documentation
  • Processing XML with Groovy
like image 75
Nicolas Modrzyk Avatar answered Sep 23 '22 05:09

Nicolas Modrzyk


The previous poster gave you all that's required: Assuming your document has been slurped into xml, you want

def foo = xml.path.to.div.find{it.@id == 'foo'} 

to find a single result. Or findAll to find all results.

like image 26
winstaan74 Avatar answered Sep 22 '22 05:09

winstaan74