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

1

9

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.

Kinsler answered 19/9, 2008 at 7:49 Comment(0)
F
18

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
Feltonfelts answered 19/9, 2008 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.