Skip to Content
avatar image
Former Member

Special Character Handling (&) in the payload content in PI 7.1

Hello All,

My Scenario is Idoc to File. I need to handle some special characters like "&" in XML payload content in PI 7.1. I am receiving & character in the text field.

eg:

<RECORD>

<BEGDA>20100901</BEGDA>

<STAT1>A</STAT1>

<NAME1>Grandview&MO CLMR</NAME1>

<PERL_SUB_AREA>0005</PERL_SUB_AREA>

</RECORD>

I created java mapping by using the blog: http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/9420. [original link is broken] [original link is broken] [original link is broken]

"The code used in java mapping is converting & into &amp"

Mapping is working fine in the test tab of Operation mapping and the message is successfully handling the record with & character(message with Chequered Flag).In the result tree view,it is showing

<PERL_TEXT>Grandview&MO CLMR</PERL_TEXT

and in the source XML view

<PERL_TEXT>"Grandview&ampMO CLMR"</PERL_TEXT>

Issue:

Now the record is written as

<RECORD>

<CUST_STATUS>A</CUST_STATUS>

<COMPANY>1050</COMPANY>

<PERL_AREA>1MOB</PERL_AREA>

<PERL_TEXT>"Grandview&ampMO CLMR"</PERL_TEXT>

<PERL_SUB_AREA>0005</PERL_SUB_AREA>

</RECORD>

Since & is coming as part of text, "it is converting & -> &amp" in the text field and the record is written in the file as

"A|1050|1MOB|Grandview&ampMO CLMR|0005 ....."

While it should come as:

A|1050|1MOB|Grandview&MO CLMR|0005 .....

Java mapping code used is as:

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Map;

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

public class HandleAmpersand implements StreamTransformation {

public void setParameter (Map param) { }

public void execute (InputStream in, OutputStream out) {

try {

int read_data;

while ((read_data = in.read() ) != -1) {

if (read_data != '&') {

out.write(read_data);

} else {

out.write("&amp;".getBytes());

}

}

out.flush();

} catch (Exception e) { }

}

}

Also, i am using message mapping after java mapping in operation mapping

Please guide me where it is going wrong.

Thanks

Shikha Jain

Edited by: Jain Shikha on Jan 5, 2011 4:42 AM

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Jan 05, 2011 at 09:22 PM
    package com.Mapping;
    
    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 Mapping_Java extends AbstractTransformation {
    	public void transform(TransformationInput transformationInput,
    			TransformationOutput transformationOutput)
    			throws StreamTransformationException {
    		try {
    			InputStream inputstream = transformationInput.getInputPayload()
    					.getInputStream();
    			OutputStream outputstream = transformationOutput.getOutputPayload()
    					.getOutputStream();
    
    			byte[] b = new byte[inputstream.available()];
    			inputstream.read(b);
    			String strContent = new String(b);
    			
    			if (strContent.contains("&")) {
    				getTrace().addInfo("& is present");
    				strContent = strContent.replaceAll("&amp ;", "&");
    
    				strContent = strContent.replaceAll("&quot ;", "\"");
    				strContent = strContent.replaceAll("&apos ;", "\'");
    				strContent = strContent.replaceAll("&amp ;", "&");
    				strContent = strContent.replaceAll("&lt ;", "<");
    				strContent = strContent.replaceAll("&gt ;", ">");
    			}
    			outputstream.write(strContent.getBytes());
    
    		} catch (Exception exception) {
    			exception.printStackTrace();
    		}
    	}
    }

    Edited by: Raghu Vamsee on Jan 6, 2011 1:59 PM

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 05, 2011 at 05:41 AM

    Hello,

    You are using :

    if (read_data != '&') {
    out.write(read_data);
    } else { 
    out.write("&".getBytes());
    }

    And blog suggests:

    if (read_data != '&') {
    out.write(read_data);
    } else { 
    out.write("&amp;".getBytes());
    }

    Please verify the code again.

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 05, 2011 at 07:37 AM

    > My Scenario is Idoc to File. I need to handle some special characters like "&" in XML payload content in PI 7.1. I am receiving & character in the text field.

    You need not do it, as IDoc adapter escapes & values automatically.

    You are waisting your time.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Dear Shikha,

      I know it was an old issue, but if you remember which Java mapping did you change? and what did you change?

      I am also having same issue...You reply will be of great help

      Thanks.

      Sumeet.

  • Jan 05, 2011 at 09:21 PM

    Hi Jain Shikha,

    I have faced similar issue in our production system.

    Our issue:-

    Sender system is sending :- DEKOR " OBS!

    Other middleware is converting it and sending to SAP PI as :- DEKOR &amp ;quot; OBS!

    Because of this, there is increase in size from 12 characters to 21 characters. We were getting error as the target side ABAP table has column width 15.

    Our solution:- Wrote a java mapping to convert &amp;quot; to u201C

    if (strContent.contains("&")) {
    				getTrace().addInfo("& is present");
    				strContent = strContent.replaceAll("&amp ;", "&");
    
    				strContent = strContent.replaceAll("&quot ;", "\"");
    				strContent = strContent.replaceAll("&apos ;", "\'");
    				strContent = strContent.replaceAll("&amp ;", "&");
    				strContent = strContent.replaceAll("&lt ;", "<");
    				strContent = strContent.replaceAll("&gt ;", ">");
    			}

    Note: - quote ("), apostrophe ('), ampersand (&), less than (<), greater than (>) are special characters in XML. They should not be present in data. http://www.w3.org/TR/REC-xml/

    Not Well formed XML:

    <Name>Raghuu2019s</Name>   <Name> Raghu & Vamsee </Name>

    Well formed XML:

    <Name>Raghu&apos;s</Name>   <Name>Raghu &amp; Vamsee</Name>

    Graphical mapping, XSLT, DOM, SAX need well formed XML as input, otherwise they give parser exception.

    Your solution:-

    I understand you are using a graphical mapping. Follow below steps

    Step 1: Convert u201CNot Well formed XMLu201D to u201CWell formed XMLu201D, using java mapping (which your doing already).

    Step 2: Use Graphical mapping for your transformation logic.

    Step 3: Again convert back u201CWell formed XMLu201D to u201CNot Well formed XMLu201D (As this what target system is expecting. Use below java mapping.

    Regards ,

    Raghu

    Edited by: Raghu Vamsee on Jan 6, 2011 1:45 PM

    Edited by: Raghu Vamsee on Jan 6, 2011 1:59 PM

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 06, 2011 at 03:54 PM

    Thanks for valuable inputs

    Add comment
    10|10000 characters needed characters exceeded