Wednesday 5 February 2020

Find the field from atom-xml / pick the field from unformatted xml in SAP CPI

Find the field from atom-xml / pick the field from unformatted xml in SAP CPI.

Data:

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="https://XXX.ondemand.com/sap/c4c/odata/cust/v1/servicelocation/" xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
  <id>https://XXX.ondemand.com/sap/c4c/odata/cust/v1/servicelocation/AddressSnapshotPostalAddressCollection</id>
  <title type="text">AddressSnapshotPostalAddressCollection</title>
  <updated>2020-02-04T10:00:13Z</updated>
  <author>
    <name/>
  </author>
  <link href="AddressSnapshotPostalAddressCollection" rel="self" title="AddressSnapshotPostalAddressCollection"/>
  <entry m:etag="W/&quot;datetimeoffset'2020-01-23T12%3A51%3A32.3360950Z'&quot;">
    <id>https://XXX.ondemand.com/sap/c4c/odata/cust/v1/servicelocation/AddressSnapshotPostalAddressCollection('00163E6CDD2F1EEA8FBBE27F8D76E336')</id>
    <title type="text">AddressSnapshotPostalAddressCollection('00163E6CDD2F1E7A8FBBE22F8D76E336')</title>
   <updated>2020-02-04T10:00:13Z</updated>
    <category term="cust.AddressSnapshotPostalAddress" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
    <link href="AddressSnapshotPostalAddressCollection('00163E6CDD2F1E7A8FBBE22F8D76E336')" rel="edit" title="AddressSnapshotPostalAddress"/>
    <content type="application/xml">
      <m:properties>
        <d:ObjectID>00163E6CDD2F1E7A8FBBE22F8D76E336</d:ObjectID>
        <d:ETag>2020-01-23T12:51:32.3360950Z</d:ETag>
        <d:CountryCode>BE</d:CountryCode>
        <d:CityName>Scherpenheuvel-Zichem</d:CityName>
        <d:StreetName>Bergstraat 1</d:StreetName>
        <d:StreetPostalCode>3272</d:StreetPostalCode>
        <d:CountyName/>
      </m:properties>
    </content>
  </entry>
</feed>

Groovy Script:

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

def Message processData(Message message) {
def body = message.getBody();
def data = new XmlSlurper().parseText(body);
String[] productId= " ";

def id = data.'**'.findAll { node -> node.name() == 'ObjectID' }*.text();
def country = data.'**'.findAll { node -> node.name() == 'CountryCode' }*.text();
def city = data.'**'.findAll { node -> node.name() == 'CityName' }*.text();

assert id.size() == 1;
assert country.size() ==1;
assert city.size() ==1;
message.setProperty("objectid",id);
message.setProperty("CountryCode",country);
message.setProperty("CityName",city);

return message;

}

Thanks Shruthi for helping on this solution.



No comments:

Post a Comment