1

I was trying to add node using Groovy script but output is showing like input.

Input:

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

Code:

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

Please help in creating new node using Groovy script.

Getting this error when XML path also:

No signature of method: groovy.util.NodeList.appendNode()

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48

1 Answers1

0

You can create a node like this so Field5 Node is added to each row inside data:

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

See the groovyide.com/cpi code.

Given the xml you provided on the link:

<?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>
Maicon Mauricio
  • 2,052
  • 1
  • 13
  • 29
  • From OP: "Hi, really thank you so much for your response. My actual requirement contains multiple repeating nodes structure as in this case, I was getting similar issue." Long Groovy IDE URL in 2 comments, part 1: https://groovyide.com/cpi/share/v1/zVRtT9swEP4rnj-1U3H6AhsrIWiUoX4Y01gpqkT44CXX1pNjZ7ZT6FD_-86JK0rh-2Ypyvl57nzne_ETFaqsHB0-0Z86X9Mhjc8eC0lWYKzQ6jSlPdZNKQGV6VyoBQLTm8uD45SeJamKb3_L6wrM-gdYh18lHaKpiw3YUisLE8ddZZPJdDT6MpnE0R7-QvcCHBeyARGWohAu6XW73Thq5EDo-dyCSxAOUsCt-APJII7qf8Ccdlx6sBFqf9FbDuOcO761MvohiLi5FCDzXvL5fBRHQX7J9ZNefxC4_h43SGazWeAGe9xhcvTh4_GnbqAPt96jZ_c7kRBc_0ss_zQrcRRKFUd... – Sandra Rossi Jul 01 '23 at 06:23
  • Part 2 of URL: ...vdh_t0CXwHPvXN7WW-bjeYWePQUr9jm46tDS6BOMEeJ0NAjYzoqynANscUFcUpTaOZLpglpdswR088DUTJcu0AZZV1ukihxWrnJDsCqzlCzhJVbD7xVe8ocbcLq94-UwtjNarNcMhY7NCTlHnFVcbvkc4VTnMSTidYNQZihd4_dYWK5p_mzw1un6IyekWZgtw54i06ngkVws2cQbHuH3SqKM_h-oKHggG850bC6bVZqUXbuDRtfx57VR5PfY63Qx4tvSuSVi3118b5iB5A2S-cq9s_PLEN8z8rtkuzrD6d917xssSVO6RVkrrBjlKaYfcDe87JKV4jbnHUurv13TV5vlAFLd5sSEvoQK4N4JLfDha_qZtb27AVUZtM4nAhpC6u-aVyhy-jdglOxWhm81f – Sandra Rossi Jul 01 '23 at 06:23
  • Hi @SandraRossi. I don't understand your comment. Can you clarify for me? Do you need something? – Maicon Mauricio Jul 01 '23 at 21:00
  • Nope, sorry for disturbing. This was a comment that the Original Poster posted 2 years ago (Jul 25, 2021), targeted to you but added inside the question instead of comment. Yesterday, I edited the question to clarify it, and moved out this part into a comment. – Sandra Rossi Jul 02 '23 at 05:35