0

I have a input xml like below where I want to extract 'Maker' value and pass it to an element:-

<?xml version="1.0" encoding="UTF-8"?>
<Data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                ../../Di/xsd/Doc/Data_0100.xsd">
    <Area>
        <CreationDateTime>2020-11-30T15:47:44Z</CreationDateTime>
        <No>
            <Id1>146520459</Id1>
        </No>
    </Area>
    <text>
        <Catalog>
            <Part>
                <Id>12345</Id>
                <Revision/>
                <Mixer>rtg</Mixer>
            </Part>
            <Dis>
                <Maker>7874</Maker>
            </Dis>
        </Catalog>
    </text>
</Data> 

I tried like below, I need to fetch element2 and pass it as property but no luck:-

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.xml.MarkupBuilder;
def Message processData(Message message) {
     def body = message.getBody()
       
       def root = new XmlSlurper().parseText(body);
       
       def element2 = root.text.Catalog.Dis.Maker;
       message.setProperty("element2", element2.text());
       return message;
}
John
  • 105
  • 9

2 Answers2

1

The path is wrong. Use root.text.Catalog.Dis.Maker instead.

edit

According to OP the actual error is:

Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.XmlSlurper.parseText() is applicable for argument types: (ByteArrayInputStream) values: [java.io.ByteArrayInputStream@489dd356]

parseText is used for parsing a String. parse can work with an InputStream.

cfrick
  • 35,203
  • 6
  • 56
  • 68
  • Hi Thanks, I tried but getting below error:- Caused by: groovy.lang.MissingMethodException: No signature of method: groovy.util.XmlSlurper.parseText() is applicable for argument types: (ByteArrayInputStream) values: [java.io.ByteArrayInputStream@489dd356] – John Feb 28 '21 at 22:54
  • Yeah it's `parse` and not `parseText`. Maybe if you would add the actual error you are getting to your question. Also tagging the question with whatever SAP environment you are running this would help. – cfrick Feb 28 '21 at 23:32
1

Correction:

 def body = message.getBody(java.lang.String)
   
kingAm
  • 1,755
  • 1
  • 13
  • 23