Groovy脚本无法正确解析XML

时间:2013-03-06 19:21:56

标签: java xml parsing groovy

我正在尝试使用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,它将具有以下行为:

  1. 首先list/list/widget/buzzfalse,所以不要做任何事情
  2. 第二个list/list/widget/buzztrue,因此请使用字符串聚合模式:
    1. 第二个list/list/widget/explanations有2个<string/>个孩子;将它们添加到主列表(masterList
  3. 第三个list/list/widget/buzztrue,因此请继续将其子字符串聚合到主列表中
    1. 第三个list/list/widget/explanations有一个<string/>孩子;将其添加到主列表(masterList
  4. masterList现在有3个字符串:第2个小部件为2个,第3个小部件为1个
  5. 到目前为止,这是我最好的尝试:

    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大师可以在哪里找到我的意思吗?提前谢谢。

1 个答案:

答案 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()