daviderossi.blogspot.com提供了一些帮助,我设法让一些代码用另一个替换xml值
def fm_xml = '''<?xml version="1.0" encoding="UTF-8"?>
<MAlong>
<Enquiry.ID>SC11147</Enquiry.ID>
<student.name_middle></student.name_middle>
<student.name_known></student.name_known>
<student.name_previous></student.name_previous>
<student.name_cert>John REnfrew</student.name_cert>
<student.detail_gender>M</student.detail_gender>
<student.sign_name>John Renfrew</student.sign_name>
<student.sign_date>05/01/2010</student.sign_date>
</MAlong>'''
xml = new XmlParser().parseText(fm_xml)
ix = xml.children().findIndexOf{it.name() =='student.name_middle'}
nn = new Node(xml, 'student.name_middle', "NEW")
if (ix != -1 ) {
xml.children()[ix] = nn
nn.parent = xml
}
writer = new StringWriter()
new XmlNodePrinter(new PrintWriter(writer)).print(xml)
result = writer.toString()
这给了我以下输出但是我希望它与正确形成的结束标记一起,否则对新数据的XPath查询将失败..
所以例如
<student.name_known/>
需要成为
<student.name_known></student.name_known>
任何想法??
<MAlong>
<Enquiry.ID>
SC11147
</Enquiry.ID>
<student.name_middle>
NEW
</student.name_middle>
<student.name_known/>
<student.name_previous/>
<student.name_cert>
John REnfrew
</student.name_cert>
<student.detail_gender>
M
</student.detail_gender>
<student.sign_name>
John Renfrew
</student.sign_name>
<student.sign_date>
05/01/2010
</student.sign_date>
<student.name_middle>
NEW
</student.name_middle>
</MAlong>
答案 0 :(得分:2)
<student.name_known/>
完美的格式化,并且XPath查询应该在此XML结构上运行良好。
答案 1 :(得分:0)
它应该通过将 expandEmptyElements 选项设置为true来实现
请参阅http://groovy.codehaus.org/api/groovy/util/XmlNodePrinter.html#setExpandEmptyElements%28boolean%29