Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

SAP PI - Deciphering apostrophes in XML.

Link to Content's target Space :

http://wiki.sdn.sap.com/wiki/display/XI/Process+Integration+-+Exchange+Infrastructure 

Applies to:

SAP PI 7.1/7.0.

Summary

This article explains how to handle special characters in file based scenarios using simple mapping programs

Author(s):  

Sreehari Nambiar   
Company:     Deloitte Consulting
Created on:    September 2, 2011
Author(s) Bio
Sreehari, currently working with Deloitte Consulting, is a certified SAP PI consultant with sound ABAP knowledge

Scenario

This interface is an ABAP proxy to XML file scenario. The XML file was generated and sent to the legacy system. However, the file failed initial validations. Reason - the field EventCode in the target contained unwanted characters (') which the legacy system could not interpret

Analysis

In the first look, everything looked good in PI. The XML, post mapping step had no junk characters. Similarly, the XML file viewed using Internet Explorer also had correct data.


 Lord Google told us that ''' is nothing but "XML Representation of apostrophes"
 This prompted us to take a look at the XML in raw format. And there we got hold of the culprit - XML parsing.

The XML parsing that happens during graphical mapping converted the apostrophe into '''. So the obvious solution was to avoid the parsing. 

Solution

This was achieved by a java mapping which transferred the the source XML as a stream to the target and while doing so, replacing the "&asos;" back to normal aposotrophe.The Java Mapping was called after the first (graphical mapping) which had the transformation rules as per business.
 

Note: Similar approach could be used for other XML characters as well. The XML file when viewed in Internet Explorer or the Graphical Mapping output, will display Apostrophes correctly. Only when you view the XML file as a raw text, these special characters are displayed as per XML standards  

Java Code
import java.io.InputStream;
import java.io.OutputStream;

import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;


public class JMap extends AbstractTransformation
{     public void transform(TransformationInput in, TransformationOutput out)
	  throws StreamTransformationException {
			try{
				  InputStream is = in.getInputPayload().getInputStream();
				  OutputStream os = out.getOutputPayload().getOutputStream();
				  long length = is.available();
				  byte[] bytes = new byte[(int) length];
				  is.read(bytes);
				  String cleanXMLString = null;
				  cleanXMLString =new String(bytes).replaceAll("'","'");
				  os.write(cleanXMLString.getBytes());
			}
			catch (Exception ex){
				  ex.printStackTrace();
			}
	  }