【发布时间】:2014-03-10 09:23:37
【问题描述】:
我想读取 xml,过滤某些特定的“类别”并将结果写入屏幕或以 xml 格式写入文件。我无法为 XmlUtil.serialize 或 StreamingMarkupBuilder 找到正确的类型。在哪里可以找到 XmlUtil.serialize 或 StreamingMarkupBuilder?
def input = '''
<shopping>
<category type="groceries">
<item>Chocolate</item>
<item>Coffee</item>
</category>
<category type="supplies">
<item>Paper</item>
<item quantity="4">Pens</item>
</category>
<category type="present">
<item when="Aug 10">Kathryn's Birthday</item>
</category>
</shopping>
'''
def root = new XmlSlurper().parseText(input)
def groceries = root.shopping.findAll{ it.@type == 'groceries' }
// here I like to print the filtered result to file/screen
/** <category type="groceries">
<item>Chocolate</item>
<item>Coffee</item>
</category>
**/
println serializeXml(root) // I would like to write here 'groceries' but the type is not something for XmlUtil.serialize or StreamingMarkupBuilder
def String serializeXml(GPathResult xml){
XmlUtil.serialize(new StreamingMarkupBuilder().bind {
mkp.yield xml
} )
}
【问题讨论】: