on 2024 Aug 12 10:25 AM
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
I'm getting the data in content modifier in exchange properties and calling them in groovy script.
My script is :
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
@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.
User | Count |
---|---|
63 | |
9 | |
8 | |
6 | |
6 | |
6 | |
6 | |
6 | |
5 | |
5 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.