Skip to Content

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

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    Feb 08 at 07:22 AM

    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());  
     }
     
     }
     
    }
    
    Add comment
    10|10000 characters needed characters exceeded