Wednesday, 13 February 2019

Find node from XML using Groovy Script


Find the particular node from the xml data by the below script.

GroovyScript:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.xml.XmlUtil;
import groovy.util.*;

def Message processData(Message message) {

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

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

def x = xml.'**'.findAll { it.name() == 'claimNumber' }

body = XmlUtil.serialize(xml)

message.setProperty("claimNum",x);

message.setBody(body);

return message;

}

Call this ${property.claimNum} whereever required to retrieve the field value of ClaimNumber.


Thanks. Happy Learning!!!



Friday, 8 February 2019

JSON to XML convertor nodes adding script

When converting the JSON data to XML using standard JSON to XML converter, the Root node and the other sub node will not be added, if there are multiple messages in the payload. This will be an issue while sending converted XML to any HTTP receivers.

So use the below script to make this work before the JSON to XML convertor

Groovy Script:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*

def Message processData(Message message) {

def jsonOP = message.getBody(String.class);

jsonOP=jsonOP.toString()

def json_to_str=jsonOP.substring(1, jsonOP.length()- 1);

json_to_str="{\"Root\": [{\"EmpRoot\":["+json_to_str+"]}]}"

message.setBody(json_to_str);

return message;
}


Input:

[
{

"EmpID": 5656,

"EmpName": "Ram Kumar",

"Grade": 5

},

{

"EmpID": 5659,

"EmpName": "Ram Kumar",

"Grade": 2
}
]

Output:

<?xml version="1.0" encoding="UTF-8" ?>

<Root>
<EmpRoot>
<EmpID>5656</EmpID>
<EmpName>Ram Kumar</EmpName>
<Grade>5</Grade>
</EmpRoot>

<EmpRoot>
<EmpID>5659</EmpID>
<EmpName>Ram Kumar</EmpName>
<Grade>2</Grade>
</EmpRoot>
<Root>



Remove all the namespace from XML using XSLT mapping

There will be an issue in XPATH handling in content modifier/Router/Filter when there is namspace (like ns2:) for all the fields.

To remove all the namespace in the XML payload, use the below XSLT mapping code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" version="1.0" encoding="UTF-8" />
        <xsl:template match="*">
                <xsl:element name="{local-name()}" >
                        <xsl:apply-templates select="@* | node()"/>
                </xsl:element>
        </xsl:template>
</xsl:stylesheet>

(or)

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes" method="xml" encoding="utf-8" omit-xml-declaration="yes"/>

    <!-- Stylesheet to remove all namespaces from a document -->
    <!-- NOTE: this will lead to attribute name clash, if an element contains
        two attributes with same local name but different namespace prefix -->
    <!-- Nodes that cannot have a namespace are copied as such -->

    <!-- template to copy elements -->
    <xsl:template match="*">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="@* | node()"/>
        </xsl:element>
    </xsl:template>


    <!-- template to copy attributes -->
    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>


    <!-- template to copy the rest of the nodes -->
    <xsl:template match="comment() | text() | processing-instruction()">
        <xsl:copy/>
    </xsl:template>

</xsl:stylesheet>

Input:

<?xml version="1.0" encoding="UTF-8"?>
<ns2:MT_TOnlineBookResponseDetails xmlns:ns2="urn:electrolux.com">
  <BookSuccessfully>true</BookSuccessfully>
  <ErrorCode>0</ErrorCode>
</ns2:MT_TOnlineBookResponseDetails>

Output:

<?xml version="1.0" encoding="UTF-8"?>
<MT_TOnlineBookResponseDetails>
  <BookSuccessfully>true</BookSuccessfully>
  <ErrorCode>0</ErrorCode>
</MT_TOnlineBookResponseDetails>