on 05-14-2018 6:32 AM
HI Experts,
Can you please suggest me any blog/scenario for Creating groovy script mapping into a target hierarchal structure if you have already done that.
Regards,
HI Morten Wittrock,
Thanks a lot for your reply ! It will be really apprecited if you could help me in the which the groovy script can be used as one to one mapping from source to target for the strcuture
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Vijender
It's not really complicated. There are two steps to it:
Assuming the content is XML, step 1 is XML parsing and step 2 is building an XML document.
Parsing XML
To parse XML in Groovy, you have the groovy.util.XmlSlurper class available. In the following example, I'll be parsing this simple document:
<lines>
<line>
<num>10</num>
</line>
<line>
<num>20</num>
</line>
<line>
<num>30</num>
</line>
</lines>
Here's a short code snippet that parses the document and sums up the numbers:
import groovy.util.XmlSlurper
def lines = new XmlSlurper().parseText('<lines><line><num>10</num></line><line><num>20</num></line><line><num>30</num></line></lines>')
def sum = 0
lines.line.num.each { num -> sum += num.text() as Integer }
println sum
To parse the message body, you would instead run:
new XmlSlurper().parseText(message.getBody(java.lang.String))
You can read more about XML parsing in Groovy here.
Building an XML document
With Groovy's groovy.xml.MarkupBuilder class, building an XML document is really easy. Here's a piece of code that generates a simple document and replaces the message body with it:
import com.sap.gateway.ip.core.customdev.util.Message
import groovy.xml.MarkupBuilder
def Message processData(Message message) {
def sw = new StringWriter()
def builder = new MarkupBuilder(sw)
builder.xmldoc {
message("Hello, world!")
}
message.setBody(sw.toString())
return message
}
The code generates the following XML:
<xmldoc>
<message>Hello, world!</message>
</xmldoc>
More MarkupBuilder examples can be found here.
So, what you need to do now, is combine the two techniques, and implement your specific requirements.
Have fun,
Morten
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
25 | |
12 | |
9 | |
6 | |
6 | |
5 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.