xmlgroovyappendsap-cpi

SAP CPI: Append node into XML with groovy


I am trying to append a node to XML message after specific element. Here is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
   <ns0:Message1>
      <ns1:BusinessPartnerSUITEBulkReplicateRequest xmlns:ns1="http://sap.com/xi/SAPGlobal20/Global">
         <BusinessPartnerSUITEReplicateRequestMessage>
            <BusinessPartner actionCode="04" addressInformationListCompleteTransmissionIndicator="true" roleListCompleteTransmissionIndicator="true">
               <UUID>b26a8b91-da4d-4761-bbc5-4bf704f0eb4a</UUID>
               <InternalID>00000016</InternalID>
               <CategoryCode>2</CategoryCode>
               <AddressInformation actionCode="04">
                  <UUID>3ccc411f-5ff3-4f78-a38a-11c4eb45d30c</UUID>
                  <Address actionCode="04" emailListCompleteTransmissionIndicator="true" facsimileListCompleteTransmissionIndicator="true">
                     <CommunicationPreference actionCode="04">
                        <CorrespondenceLanguageCode>DE</CorrespondenceLanguageCode>
                     </CommunicationPreference>
                     <Email actionCode="04" eMailUsageListCompleteTransmissionIndicator="true">
                        <URI>test@sap.com</URI>
                        <UsageDeniedIndicator>false</UsageDeniedIndicator>
                        <EmailUsage actionCode="04">
                           <Usage>
                              <Code>AD_DEFAULT</Code>
                           </Usage>
                           <DefaultIndicator>true</DefaultIndicator>
                        </EmailUsage>
                     </Email>
                     <PostalAddress actionCode="04">
                        <CountryCode>UA</CountryCode>
                        <RegionCode />
                        <CityName>Bönen</CityName>
                        <StreetPostalCode>59199</StreetPostalCode>
                        <StreetName>Siemensstraße</StreetName>
                        <HouseID>10</HouseID>
                        <POBoxID />
                     </PostalAddress>
                  </Address>
               </AddressInformation>
               <Common actionCode="04">
                  <Organisation>
                     <Name>
                        <FirstLineName>testfirstname</FirstLineName>
                        <SecondLineName>testlastname</SecondLineName>
                     </Name>
                  </Organisation>
               </Common>
               <Role actionCode="04">
                  <RoleCode>ZCU005</RoleCode>
               </Role>
            </BusinessPartner>
         </BusinessPartnerSUITEReplicateRequestMessage>
         <BusinessPartnerSUITEReplicateRequestMessage>
            <BusinessPartner actionCode="04" addressInformationListCompleteTransmissionIndicator="true" commonListCompleteTransmissionIndicator="true" roleListCompleteTransmissionIndicator="true">
               <UUID>ace66c2d-44a8-4238-b411-53ad2520d7b4</UUID>
               <InternalID>DUMMY</InternalID>
               <CategoryCode>1</CategoryCode>
               <AddressInformation actionCode="04">
                  <UUID>eb16d063-faf5-4fbb-a67c-514216ee162f</UUID>
                  <Address actionCode="04" emailListCompleteTransmissionIndicator="true" facsimileListCompleteTransmissionIndicator="true">
                     <CommunicationPreference actionCode="04">
                        <CorrespondenceLanguageCode>DE</CorrespondenceLanguageCode>
                     </CommunicationPreference>
                     <PersonName actionCode="04">
                        <Name>
                           <GivenName>DUMMYfirstname</GivenName>
                           <FamilyName>DUMMYlastname</FamilyName>
                        </Name>
                     </PersonName>
                  </Address>
               </AddressInformation>
               <Role actionCode="04">
                  <RoleCode>BUP001</RoleCode>
               </Role>
            </BusinessPartner>
         </BusinessPartnerSUITEReplicateRequestMessage>
      </ns1:BusinessPartnerSUITEBulkReplicateRequest>
   </ns0:Message1>
</ns0:Messages>

I am trying to append

<MessageHeader><SenderBusinessSystemID>"+sender+"</SenderBusinessSystemID><RecipientBusinessSystemID>"+receiver+"</RecipientBusinessSystemID></MessageHeader>

after node BusinessPartnerSUITEBulkReplicateRequest, before BusinessPartnerSUITEReplicateRequestMessage, but it does not append it.

My code is:

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


def Message processData(Message message) {
    
    def ns0 = new groovy.xml.Namespace('http://sap.com/xi/XI/SplitAndMerge');
    def ns1 = new groovy.xml.Namespace('http://sap.com/xi/SAPGlobal20/Global');
    def receiver = message.getProperties().get("Receiver");
    def sender = message.getProperties().get("Sender");
    def body = message.getBody(java.lang.String) as String;
    def newString = "<MessageHeader><SenderBusinessSystemID>"+sender+"</SenderBusinessSystemID><RecipientBusinessSystemID>"+receiver+"</RecipientBusinessSystemID></MessageHeader>"

    def response= new XmlSlurper().parseText(body)
    def newNode = new XmlSlurper().parseText(newString)     
    
response.Messages[0].Message1[0].BusinessPartnerSUITEBulkReplicateRequest[0].appendNode(newNode);
response.appendNode(newNode);
def String outxml = groovy.xml.XmlUtil.serialize(response)
    
  message.setBody(outxml);      
 
  return message;
}

If I append it directly with response.appendNode - it works - it appends it at the end of the message as expected. I tried also using namespaces to point the correct location, but it does not work as well. response.[ns0.Messages][0].Message1[0].[ns1.BusinessPartnerSUITEBulkReplicateRequest][0].appendNode(newNode)

Any ideas would be much appreciated!


Solution

  • If you want to modify the xml tree I think you should use XmlParser instead of XmlSlurper. It is quite similar, but works with Node instead of GPathResult.

        def response= new XmlParser().parseText(body)
        def newNode = new XmlParser().parseText(newString)
    
        def node = response[ns0.Message1][0][ns1.BusinessPartnerSUITEBulkReplicateRequest][0]
        // node.append(newNode)         // use this if the order doesn't matter
        node.children().add(0, newNode) // children() returns a list