给定一个HTML文件结构html - > body - >一堆div什么是正确的groovy语句,找到所有具有非空标签属性的div?
以下不起作用:
def nodes = html.body.div.findAll { it.@tags != null }
Run Code Online (Sandbox Code Playgroud)
因为它找到了所有节点.
小智 18
尝试以下(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 { it.@tags.text()}.each { div ->
println div.text()
}
Run Code Online (Sandbox Code Playgroud)
这输出:
test1
test4
Run Code Online (Sandbox Code Playgroud)