我想使用Groovy XmlSlurper添加"如下所示的XML的所有字段值的开始和结束。
<SAPEmployeeData>
<record>
<COMPANY>060</COMPANY>
<VENDOR>E010013787</VENDOR>
<EMPLID>10013787</EMPLID>
<EFFECT_DATE>20180401</EFFECT_DATE>
<LAST_NAME>GONZALEZ-VARELA</LAST_NAME>
</record>
<record>
<COMPANY>060</COMPANY>
<VENDOR>E010013788</VENDOR>
<EMPLID>10013788</EMPLID>
<EFFECT_DATE>20140101</EFFECT_DATE>
<LAST_NAME>CARDOZO</LAST_NAME>
</record>
</SAPEmployeeData>
- &GT;
<SAPEmployeeData>
<record>
<COMPANY>"060"</COMPANY>
<VENDOR>"E010013787"</VENDOR>
<EMPLID>"10013787"</EMPLID>
<EFFECT_DATE>"20180401"</EFFECT_DATE>
<LAST_NAME>"GONZALEZ-VARELA"</LAST_NAME>
</record>
<record>
<COMPANY>"060"</COMPANY>
<VENDOR>"E010013788"</VENDOR>
<EMPLID>"10013788"</EMPLID>
<EFFECT_DATE>"20140101"</EFFECT_DATE>
<LAST_NAME>"CARDOZO"</LAST_NAME>
</record>
</SAPEmployeeData>
我认为代码会是这样的。但我不知道在replaceNode部分写什么。有人能帮帮我吗?
def Message processData(Message message) {
//Body
def body = message.getBody(java.lang.String) as String;
def xmlString = new XmlSlurper().parseText(body)
// filter on date
def items = xmlString.record.replaceNode{ something in this }
// Create output body
def newbody = new StreamingMarkupBuilder().bind {mkp.yield xmlString}.toString()
message.setBody(newbody)
return message;
}
答案 0 :(得分:1)
试试这个:
xmlString.children().each{ record->
record.childNodes().each{
it.replaceBody("\"${it.text()}\"")
}
}