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()
。這是預期的行爲,還是我錯過了文檔中的某些內容?