gpath

How to find element by attribute value in GPath?

佐手、 提交于 2019-12-03 22:25:43
What is an alternative to this XPath //div[@id='foo'] in GPath? In general, where I can find this documentation? Nicolas Modrzyk 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 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. To mimic the

Get value of an XML attribute with Groovy (gpath)

拜拜、爱过 提交于 2019-12-01 10:45:53
Using XmlParser() in groovy. See the following code. I need to print the value of answer when the value of name is type . <root> <foo name = 'type' answer = 'car'/> <foo name = 'color' answer = 'red'/> <foo name = 'size' answer = 'big'/> </root> I need to do something like this: def XML = new XmlParser().parseText(XMLstring) println XML.root.foo.[where @name = 'type'].@answer I can't tell if you expect there to be multiple matches or if you know there will be exactly one. The following will find them all and print their answer. source = ''' <root> <foo name = 'type' answer = 'car'/> <foo name

Get value of an XML attribute with Groovy (gpath)

天涯浪子 提交于 2019-12-01 08:23:57
问题 Using XmlParser() in groovy. See the following code. I need to print the value of answer when the value of name is type . <root> <foo name = 'type' answer = 'car'/> <foo name = 'color' answer = 'red'/> <foo name = 'size' answer = 'big'/> </root> I need to do something like this: def XML = new XmlParser().parseText(XMLstring) println XML.root.foo.[where @name = 'type'].@answer 回答1: I can't tell if you expect there to be multiple matches or if you know there will be exactly one. The following