我有xml文件,其中包含数千个xml节点。在我的groovy代码中,我想提取其中的N个并推送到另一个文件中。我如何用groovy做到这一点? 一些样本列表:
def ibr = new File('/Users/alex/Downloads/temp.xml');
def ibrParser = new XmlParser().parseText(ibr.getText());
def groups = [];
int current = 0;
ibrParser.each { group ->
if (current <= 100) {
groups<<group
}
current++
}
//how to store groups as xml into another file?
答案 0 :(得分:0)
以下代码帮助了我:
int current = 0;
def nodeToSerialize;
ibrParser.each { renewalQuote ->
if (current <= 100) {
if (nodeToSerialize == null) {
nodeToSerialize = renewalQuote;
} else {
nodeToSerialize.append(renewalQuote)
}
}
current++
}
XmlUtil.serialize(nodeToSerialize, new FileOutputStream("/Users/alex/Downloads/100Cases.xml"))