Skip to Content
0

How to handle special character like '&' in coming payload in PI

Feb 07 at 12:56 PM

242

avatar image
Former Member

Dear All,

My scenario is SOAP to Proxy sync

I need to handle some special characters like "&" in XML payload content in PI 7.4 single stack. We are receiving & special character for one field

Field : Cutomer_name = R.K & Motors;

while Testing the data from SOAPUI getting error in SOAPUI like,

"Can't parse the document; nested exception caused by: Can't parse the document; nested exception caused by: org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference."

Can i use any java / xslt mapping for this, or any other solution is there.

if need to use java/ xslt mapping please provide code for this if there.

Thanks & regards,

Ramu

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Chih-Chieh Chan Feb 08 at 07:22 AM
0

HI,

The best way I think is to request your data provider a valid xml format file. "&" character is making your sender xml file invalid. To escape this kind of special characters is to put those special characters into "<![CDATA[xxxxxx]]>" structure where xxxxxx is placeholder for special characters.

Or

you could insert a java class in front of formal message mapping to exchange those characters into some reserved codes. Like this:

The java code is like this:

import java.io.*;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
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 EscapeSpecialChar extends AbstractTransformation
{
 public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)
                                                                        throws StreamTransformationException
 {
  
    InputStream inputstream = transformationInput.getInputPayload().getInputStream();
    OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();
 String inputXML = "";
 try {
  BufferedReader reader = new BufferedReader(new InputStreamReader(inputstream));
  String temp = "";
  
  while((temp = reader.readLine()) != null)
  {
   if (temp != null)
   inputXML += temp;
  }
  
 
  String outXML = inputXML.replaceAll("&", "&");
 
  
  outputstream.write(outXML.getBytes());
 } catch (Exception eex) {
  throw new StreamTransformationException(eex.toString());  
 }
 
 }
 
}

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Thank you so much Chan for your reply,.... My issue is solved

0