Groovy:XMLSlurper的正确语法可以找到具有给定属性的元素 [英] Groovy: Correct Syntax for XMLSlurper to find elements with a given attribute
本文介绍了Groovy:XMLSlurper的正确语法可以找到具有给定属性的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下行不通:
def nodes = html.body.div。 findAll {it。@ tags!= null}
因为它找到了所有的节点。
解决方案
请尝试以下内容(Groovy 1.5.6):
def doc =
< html>
< body>
< div tags =1> test1< / div>
< div> test2< / div>
< div> => test3< / div>
< div标签=4> test4< < / body>
< / html>
def html = new XmlSlurper()。parseText(doc)
$ b $ html .body.div.findAll {it。@ tags.text()}。each {div - >
println div.text()
}
输出:
test1
test4
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.
解决方案
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 { it.@tags.text()}.each { div ->
println div.text()
}
This outputs:
test1
test4
这篇关于Groovy:XMLSlurper的正确语法可以找到具有给定属性的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文