【问题标题】:Groovy script to Add node in SAP CPI在 SAP CPI 中添加节点的 Groovy 脚本
【发布时间】:2021-09-30 22:13:15
【问题描述】:

我尝试使用 groovy 脚本添加节点,但输出显示为输入。

输入:

<?xml version='1.0' encoding='UTF-8'?>
  <Records>
     <Line>
     <Field1>ABC</Field1>
     <Field2>123</Field2>
     <Field3>XXX</Field3>
     <Field4>567890</Field4>
     </Line>
   </Records>

代码:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;``
import groovy.xml.XmlUtil;
import groovy.util.*;

def Message processData(Message message) {
//Body 
   def body = message.getBody(java.lang.String) as String;
   def root = new XmlParser().parseText(body);
   root.Line[0].appendNode("Field5", [:], "MyNewField");
   return message;
}  

请帮助使用 groovy 脚本创建新节点

当 xml 路径也“在此处输入代码”时出现此错误 方法没有签名:groovy.util.NodeList.appendNode()

非常感谢您的回复。我的实际需求包含多个重复节点结构,如下 groovy IDE link. 请看下面的代码示例。我遇到了类似的问题,如下所示。

【问题讨论】:

    标签: groovy integration middleware sap-cloud-platform


    【解决方案1】:

    您可以像这样创建一个节点,以便将Field5 节点添加到数据内的每一行:

    import com.sap.gateway.ip.core.customdev.util.Message;
    import java.util.HashMap;
    import groovy.xml.XmlUtil;
    import groovy.util.*;
    
    def Message processData(Message message) {
        def body = message.getBody(java.lang.String);
        def root = new XmlParser().parseText(body)
        root.data.row.each { row ->
            row.appendNode("Field5", "newfield")
        }
        message.setBody(XmlUtil.serialize(root));
        return message;
    }
    

    the groovyide.com/cpi code

    鉴于您在链接上提供的 xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <VqlQueryRestResult>
        <responseStatus>SUCCESS</responseStatus>
        <responseDetails>
            <limit>1000</limit>
            <offset>0</offset>
            <size>3</size>
            <total>3</total>
        </responseDetails>
        <data>
            <row>
                <Field1>ABC</Field1>
                <Field2>123</Field2>
                <Field3>XXX</Field3>
                <Field4>567890</Field4>
            </row>
            <row>
                <Field1>ABC</Field1>
                <Field2>123</Field2>
                <Field3>XXX</Field3>
                <Field4>567890</Field4>
            </row>
            <row>
                <Field1>ABC</Field1>
                <Field2>123</Field2>
                <Field3>XXX</Field3>
                <Field4>567890</Field4>
            </row>
        </data>
    </VqlQueryRestResult>
    

    【讨论】:

      猜你喜欢
      • 2021-11-22
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多