1
处理XML部分中的Groovy documentation提到breadthFirst()
与*
的较短语法是同义的。然而,*
使用最终只遍历父节点下一层:Groovy-XML Tree Traversal使用*作为语法糖的breadthFirst()方法
def books = '''\
<response>
<books>
<book available="20" id="1">
<title>foo</title>
<author id="1">foo author</author>
</book>
<book available="14" id="2">
<title>bar</title>
<author id="2">bar author</author>
</book>
</books>
</response>'''
def response = new XmlSlurper().parseText(books)
def bk = response.'*'.find { node ->
node.name() == 'book' && node['@id'].toInteger() == 2
}
assert bk.empty
而使用breadthFirst()
明确做什么,我都期盼做这做广度优先遍历:
def books = '''\
<response>
<books>
<book available="20" id="1">
<title>foo</title>
<author id="1">foo author</author>
</book>
<book available="14" id="2">
<title>bar</title>
<author id="2">bar author</author>
</book>
</books>
</response>'''
def response = new XmlSlurper().parseText(books)
def bk = response.breadthFirst().find { node ->
node.name() == 'book' && node['@id'].toInteger() == 2
}
assert bk.title == 'bar' // bk is no longer an empty list of children
*
语义明显不同于breadthFirst()
。这是预期的行为,还是我错过了文档中的某些内容?