Groovy XmlSlurper添加"对所有价值观

时间:2018-06-15 04:54:58

标签: xml groovy xmlslurper

我想使用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;
}

1 个答案:

答案 0 :(得分:1)

试试这个:

xmlString.children().each{ record->
record.childNodes().each{ 
  it.replaceBody("\"${it.text()}\"")
 }
}