xpath from groovy instead of xpath in cm , some time cm not fetch value , sap cpi

 

case 1

import com.sap.gateway.ip.core.customdev.util.Message;

def Message processData(Message message) {
    def body = message.getBody(String) as String

    // Extract DocumentNo using regex
    def matcher = body =~ /<ns0:DocumentNo>(.*?)<\/ns0:DocumentNo>/
    def documentNo = matcher ? matcher[0][1] : ""

    // Optional: Set it as a header or property if needed
    message.setHeader("DocumentNo", documentNo)

    return message
}


case 2.


import com.sap.gateway.ip.core.customdev.util.Message;

import groovy.xml.XmlSlurper;

import groovy.xml.Namespace;


def Message processData(Message message) {

    def body = message.getBody(java.lang.String) as String;


    // Parse XML

    def xml = new XmlSlurper().parseText(body);


    // Define namespace used in Payload > SalesHeaders

    def ns0 = new groovy.xml.Namespace("http://schemas.royalcanin.com/BizTalk/2012/Pub/SalesOrderExport_SAL_006_24", 'ns0');


    // Extract DocumentNo

    def docNo = xml.Payload[ns0.SalesHeaders].SalesHeader[ns0.DocumentNo].text();


    // Set it as header

    message.setHeader("DocumentNo", docNo);


    return message;

}



case 3:


import com.sap.gateway.ip.core.customdev.util.Message

import groovy.xml.XmlSlurper

import groovy.xml.Namespace


Message processData(Message message) {

    // Get XML payload as string

    def body = message.getBody(String)

    

    // Parse XML

    def xml = new XmlSlurper(false, false).parseText(body)

    

    // Define namespace used in the Payload section

    def ns0 = new Namespace("http://schemas.royalcanin.com/BizTalk/2012/Pub/SalesOrderExport_SAL_006_24", 'ns0')

    

    // Extract DocumentNo from first SalesHeader

    def documentNo = xml.Payload[ns0.SalesHeaders].SalesHeader[0][ns0.DocumentNo]?.text()

    

    // Set as header if value exists

    if (documentNo) {

        message.setHeader("DocumentNoHeader", documentNo)

    }


    return message

}



sample input file



Popular posts from this blog

SAP CPI : camle expression in sap cpi , cm, router, filter and groovy script. format

pss book: గురు ప్రార్థనామంజరి . completed 21st july 2024

pss book : శ్రీకృష్ణుడు దేవుడా, భగవంతుడా completed , second review needed. 26th April 2024