Groovy XmlSlurper 将 " 添加到所有值

Groovy XmlSlurper add " to all values

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

->

<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;
}

试试这个:

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