我正在尝试使用Groovy来解析以下XML:
<list>
<list>
<widget>
<fizz id="3" />
<buzz>false</buzz>
<explanations/>
</widget>
<widget>
<fizz id="3" />
<buzz>true</buzz>
<explanations>
<string>What is the meaning of life?</string>
<string>I like the color blue.</string>
</explanations>
</widget>
<widget>
<fizz id="45" />
<buzz>true</buzz>
<explanations>
<string>I could really go for some pizza right now.</string>
</explanations>
</widget>
</list>
</list>
如果<widget/>
元素具有true
<buzz/>
子元素,则它将开始将所有explanations/string
个子元素聚合为母版List<String>
。因此,给定上面的示例XML,它将具有以下行为:
list/list/widget/buzz
是false
,所以不要做任何事情list/list/widget/buzz
为true
,因此请使用字符串聚合模式:
list/list/widget/explanations
有2个<string/>
个孩子;将它们添加到主列表(masterList
)list/list/widget/buzz
为true
,因此请继续将其子字符串聚合到主列表中
list/list/widget/explanations
有一个<string/>
孩子;将其添加到主列表(masterList
)masterList
现在有3个字符串:第2个小部件为2个,第3个小部件为1个到目前为止,这是我最好的尝试:
boolean buzzesExist = false;
List<String> masterList = new ArrayList<String>();
use(DOMCategory) {
element.normalize();
element.'widget'.each { widget ->
// If widget/buzz is true, then buzzes exist.
if(widget.'buzz'.text) {
buzzesExist = true;
}
// If buzzes exist, then aggregate all explanation strings into
// into "masterList".
if(buzzesExist) {
for(String exp : widget.'explanations')
masterList.add(exp);
}
}
这会运行,但导致masterList
包含各种奇怪的DOM内容(对于我来说太大而无法粘贴)。任何Groovy大师可以在哪里找到我的意思吗?提前谢谢。
答案 0 :(得分:1)
为什么不使用XmlParser?
更新:
list = new XmlParser().parseText xml
widgetWithExplanations = list.breadthFirst()
.findAll { it.buzz.text() == "true" }
masterList = widgetWithExplanations
.collect { it.explanations.string*.text() }
.flatten()
assert masterList == [
"What is the meaning of life?",
"I like the color blue.",
"I could really go for some pizza right now."]
emptyExplanations = widgetWithExplanations
.count { !it.explanations.string }
assert emptyExplanations == 0
否则你的domcategory可能在for循环中缺少exp.text()
。