cancel
Showing results for 
Search instead for 
Did you mean: 

How to parse an XML and check if a certain node exists in the XML or not using groovy script?

Shreyas_01
Explorer
0 Kudos
275

Hi everyone,

I've a requirement, where I have to parse an XML file and check if a certain node exists in the XML file or not, as I'm new to groovy I do not know how to proceed with this.

for examlple

<DELVRY03>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<TABNAM>EDI_DC40</TABNAM>
<MANDT>100</MANDT>
<DOCNUM>0000000000000</DOCNUM>
<DOCREL>111</DOCREL>
<STATUS>22</STATUS>
<DIRECT>3</DIRECT>
<OUTMOD>2</OUTMOD>
<IDOCTYP>DELVRY03</IDOCTYP>
<MESTYP>DESADV</MESTYP>
<CIMTYP>XXXX</CIMTYP>
</EDI_DC40>
</IDOC>
</DELVRY03>

Some Payload will have the CIMTYP node some won't, I do not want to check if the node carries any data, want to check if the node itself is present or not.

<DELVRY03>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<TABNAM>EDI_DC40</TABNAM>
<MANDT>100</MANDT>
<DOCNUM>0000000000000</DOCNUM>
<DOCREL>111</DOCREL>
<STATUS>22</STATUS>
<DIRECT>3</DIRECT>
<OUTMOD>2</OUTMOD>
<IDOCTYP>DELVRY03</IDOCTYP>
<MESTYP>DESADV</MESTYP>
</EDI_DC40>
</IDOC>
</DELVRY03>

based on that I want to execute certain conditions

Shreyas_01_0-1723454376098.png

I'm getting the data in content modifier in exchange properties and calling them in groovy script.

My script is :

 

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.lang.String;
import groovy.util.XmlSlurper
import groovy.xml.XmlUtil
 
 
def Message processData(Message message) {
 
def body = message.getBody(java.lang.String) as String;
def payload = new XmlSlurper().parseText(body);
 
String MESTYP = message.getProperty('MESTYP');
 
String IDOCTYP = message.getProperty('IDOCTYP');
 
String CIMTYP = message.getProperty('CIMTYP');
 
def date = new Date()
def dtFormat = "yyyyMMddHHmmss"
def dt = date.format(dtFormat, TimeZone.getTimeZone('IST'))
 
if(CIMTYP) {  //I require a condition to check this 
 
 String a = MESTYP + "." + IDOCTYP + "_" + dt + ".XML";
 
} else{ 
    
String a = MESTYP + "." + IDOCTYP + "." + CIMTYP + "_" + dt + ".XML"; 
}
 
message.setProperty("FileName1",a);
 
return message; }
 
Thank you in advance.
Shreyas
 
 
 
 

 

View Entire Topic
Ryan-Crosby
Active Contributor
0 Kudos

Your XPath expressions are incorrect for reading the three values.  They should be in the format //IDOC[1]/EDI_DC40/CIMTYP or //IDOC/EDI_DC40/CIMTYP.  One other thing to note is that your script uses XMLSlurper for reading the payload, but then it doesn't do anything with the object.

 

Regards,

Ryan Crosby

Shreyas_01
Explorer
0 Kudos

Hi Rayn,

Thank you for your Input, actually you can also write the Xpath expressions the way I have written, I tested the script it works that way, I've managed to parse the payload as well, I'm just stuck at the if condition part, how shall I write the condition to check if the node exists or not?

Thanks

Shreyas 

Ryan-Crosby
Active Contributor
0 Kudos

@Shreyas_01I tend to forget the wildcard format of // but change your expression from if(CIMTYP) to if(!CIMTYP).  That way when null is returned in the XPath expression for CIMTYP not existing your code will execute the proper if block.