cancel
Showing results for 
Search instead for 
Did you mean: 

XML encoding using udf java code

Former Member
0 Kudos

Hi Experts,

we are working on SAP PI version 7.0.

We are getting a SOAP response from 3rd party system.. as per below screen shot.


We need to read the data in field "DATA"and we have to send same data as request to ECC through proxy.

please suggest the java mapping code to read the XML file data which is present in the "DATA" field and parse it to target structure.
please let me know if any info required.
Thanks in advance.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hello,

U can use XSLT mapping to convert xml string into XML structure and then map the required fields to the proxy response.

In ur response mapping u can use xslt mapping followed by the graphical one (for mapping converted XML structure to the proxy response)

Thanks

Amit Srivastava

Former Member
0 Kudos

Hi Amit,

No luck we are getting error as below

Please help to get the code for the same.

Thanks in advance.

Former Member
0 Kudos

Hello,

It's a straight forward XSLT, not sure where u are struggling?

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

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

   <xsl:output omit-xml-declaration="yes"/>

   <xsl:template match="@*|node()">

      <xsl:copy>

         <xsl:apply-templates select="@*|node()"/>

      </xsl:copy>

   </xsl:template>

   <xsl:template match="/">

        <xsl:value-of select="//Data" disable-output-escaping="yes"/>

   </xsl:template>

</xsl:stylesheet>

Note - adjust namespaces according to ur structure

Thanks

Amit Srivastava

Former Member
0 Kudos

Hi Amit,

Thanks a lot for the quick response.

I have followed your steps properly now and its working fine.

I am in dilemma how the simple code is working for the complicated scenario .

Please provide me some reference links to get acquainted with the XSLT tags.

Regards,

Chaitanya

Former Member
0 Kudos

Hi Amit,

Its got worked intially and facing the below mentioned problem now.

Once after saving the interface mapping its prompming with below window.

Could you please suggest me the option to choose.

If i select the UTF-8 its giving the error "XML not well-formed".

Regards,

Chaitanya

markangelo_dihiansan
Active Contributor
0 Kudos

Hi,

Choose cancel first and then click src on the result. You will know why the XML was not-well formed.

Regards,

Mark

Former Member
0 Kudos

Hi Amit,

I have tried with the XSL code and its generating the XML structure as per the data maintained in the "Data" field.

The problem is, the input structure is static and the data present in the "Data" field is dynamic.

The output XML structure is getting generated with the data present in the "Data" field properly as per the provided screenshot.

I cant send the same output different XMl structures to ECC as there should be fixed structure in the proxy to be written.

Could you please provide me the code which will create the Message Type with the namespace, so that i can use the same in the next message mapping after the XSLT mapping and do the necessary modifications.

Regards,

Chaitanya

Former Member
0 Kudos

Hello,

U need a little bit of adjustment in xslt , check below code

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

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

   <xsl:template match="@*|node()">

      <xsl:copy>

         <xsl:apply-templates select="@*|node()"/>

      </xsl:copy>

   </xsl:template>

   <xsl:template match="/">

      <ns0:MT_Name>

         <xsl:variable name="data">

            <xsl:value-of select="substring(//Data, 39)"/>

         </xsl:variable>

         <xsl:value-of select="$data" disable-output-escaping="yes"/>

      </ns0:MT_Name>

   </xsl:template>

</xsl:stylesheet>

Thanks

Amit Srivastava

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi experts,

Can someone help me with code.

Its a priority requirement.

Thnaks,

chaitanya

Former Member
0 Kudos

Hi Chaitanya,

Please follow this below link it may help you.


Thanks,

Durga

markangelo_dihiansan
Active Contributor
0 Kudos

Here is the Java Mapping code


package ia_getcdata;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Map;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CharacterData;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import com.sap.aii.mapping.api.StreamTransformation;

import com.sap.aii.mapping.api.StreamTransformationException;

public class ExtractData implements StreamTransformation {

     public void setParameter(Map arg0) {

}

     public void execute(InputStream in, OutputStream out) throws StreamTransformationException {

          try {

               DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

               DocumentBuilder builder = factory.newDocumentBuilder();

               Document doc = builder.parse(in);

               NodeList nLData = doc.getElementsByTagName("Data");

               Node nData = nLData.item(0);

               Node child = nData.getFirstChild();

               CharacterData cd = (CharacterData)child;

               String dataString = cd.getData();

               out.write(dataString.getBytes("UTF-8"));

          }

          catch (Exception e) {

               e.printStackTrace();

           }

     }

}

         

Of course this is unoptimized, but it will work for your requirement.

Regards,

Mark

Former Member
0 Kudos

Hi Mark,

I have tried with the java mapping provided by you and its get worked as per requirement..

Thanks alot for the response .

The problem is, the input structure is static and the data present in the "Data" field is dynamic.

The output XML structure is getting generated with the data present in the "Data" field properly as per the provided screenshot.

I cant send the same output different XMl structures to ECC as there should be fixed structure in the proxy to be written.

Could you please provide me the code which will create the Message Type with the namespace, so that i can use the same in the next message mapping after the java mapping and do the necessary modifications.

Regards,

Chaitanya

markangelo_dihiansan
Active Contributor
0 Kudos

Hi,

Sorry for the late response. Here is the new code:


package ia_getcdata;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Map;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CharacterData;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import com.sap.aii.mapping.api.StreamTransformation;

import com.sap.aii.mapping.api.StreamTransformationException;

public class ExtractData implements StreamTransformation {

     /**

      

@see com.sap.aii.mapping.api.StreamTransformation#setParameter(Map)

      */

     public void setParameter(Map arg0) {

     }

     public void execute(InputStream in, OutputStream out) throws StreamTransformationException {

          try {

             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

             DocumentBuilder builder = factory.newDocumentBuilder();

             Document doc = builder.parse(in);

             NodeList nLData = doc.getElementsByTagName("Data");

             Node nData = nLData.item(0);

             Node child = nData.getFirstChild();

             CharacterData cd = (CharacterData)child;

             String dataString = cd.getData();

             dataString = dataString.substring(dataString.indexOf("?>")+2);

  

             out.write("<YourMessageTypeHere>".getBytes("UTF-8"));

             out.write(dataString.getBytes("UTF-8"));

             out.write("</YourMessageTypeHere>".getBytes("UTF-8"));

  

          }

          catch (Exception e) {

               e.printStackTrace();

          }

     }

}

Hope this helps,

Mark