Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Groovy: Correct Syntax for XMLSlurper to find elements with a given attribute

Given a HTML file with the structure html -> body -> a bunch of divs what is the correct groovy statement to find all of the divs with a non blank tags attribute?

The following is not working:

def nodes = html.body.div.findAll { it.@tags != null }

because it finds all the nodes.

like image 575
Peter Kelley Avatar asked Sep 19 '08 07:09

Peter Kelley


People also ask

What is groovy XmlSlurper?

Groovy's internal XmlParser and XmlSlurper provide access to XML documents in a Groovy-friendly way that supports GPath expressions for working on the document. XmlParser provides an in-memory representation for in-place manipulation of nodes, whereas XmlSlurper is able to work in a more streamlike fashion.

What is XML slurper?

@Deprecated public class XmlSlurper extends DefaultHandler. Parse XML into a document tree that may be traversed similar to XPath expressions. For example: def rootNode = new XmlSlurper().parseText( '<root><one a1="uno!"/>< two>Some text!</


1 Answers

Try the following (Groovy 1.5.6):

def doc = """
<html>
    <body>
        <div tags="1">test1</div>
        <div>test2</div>
        <div tags="">test3</div>
        <div tags="4">test4</div>
    </body>
</html>
"""

def html = new XmlSlurper().parseText( doc)

html.body.div.findAll { [email protected]()}.each { div ->
    println div.text()
}

This outputs:

test1
test4
like image 149
GerG Avatar answered Sep 21 '22 17:09

GerG