cancel
Showing results for 
Search instead for 
Did you mean: 

how to remove special char in message mapping

former_member229036
Participant
0 Kudos

Hi

in SAP PI,   we got the error Character reference "&#x1F" is an invalid XML character.

i would know how to remove special char in message mapping 

is there a way of using UDF  or graphical mapping or  java mapping ?

please help me.

Thank You

David Nail.

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi,

Please provide your source xml.

BR,

Anirban

iaki_vila
Active Contributor
0 Kudos

Hi David,

You can use a java mapping to remove it. This blog could be useful for you , you only need to change the escape sequence by you char.

Also, you can check the java codes pointed to by several SCN members in this thread

Regards.

former_member229036
Participant
0 Kudos

Hi guru;

udf is not possible to remove it?

Regards.

iaki_vila
Active Contributor
0 Kudos

Hi David,

As far as i know with udf is not possible because the message mapping parses the inputstream before any function will be executed therefore the exception is before any possible udf can be executed.

However, the java mapping does't try this parse, you have directly the bytes in the stream, then you can avoid the parse exception skipping the special characters.

Regards.

former_member229036
Participant
0 Kudos

Hi Iñaki Vila

thank you for reply information .

could you help me the following java source for java mapping?

public void execute(InputStream inStream, OutputStream outStream)   {

    try{

        BufferedReader in = new BufferedReader(new InputStreamReader(inStream));

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(outStream));

       

        String spc1 = "&#x1F";

     

        // i do not know 

               ????????

        }

        in.close();

        out.close();                           

  }catch(Exception e){

        e.printStackTrace();

  }

          

}

Regards;

iaki_vila
Active Contributor
0 Kudos

Hi David,

I you pay attention to the first link that i mentioned you have a part of the code that do:


           //Replaces control characters with an empty string.

           String result = m.replaceAll("?"); 

you only need to set you special character inside the replaceAll.

Hope this helps.

Regards.

former_member229036
Participant
0 Kudos

HI java guru ;

could you help me java code how to remove spc1 on the following code ?  sorry i am not java guy..

please understand me.

how to remove

try{

        BufferedReader in = new BufferedReader(new InputStreamReader(inStream));

        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(outStream));

      

        String spc1 = "&#x1F";

    

        // i do not know how to remove spc1 by java code

             // ????????

        }

        in.close();

        out.close();                          

  }catch(Exception e){

        e.printStackTrace();

  }

Regards;

Former Member
0 Kudos

Hi David,

Please use below java code, it will replace all control characters in xml file.

public void execute(InputStream inStream, OutputStream outStream) throws StreamTransformationException{

          try{

               BufferedReader in = new BufferedReader(new InputStreamReader(inStream));

               BufferedWriter out = new BufferedWriter(new OutputStreamWriter(outStream));

    

               // The pattern matches control characters

               Pattern p = Pattern.compile("[^ -~]");

    

               Matcher m = p.matcher("");

               String aLine = null;

               while((aLine = in.readLine()) != null) {

                    m.reset(aLine);

                    //Replaces control characters with an empty string.

                    String result = m.replaceAll("");         

                    out.write(result);

                    out.newLine();

               }

               in.close();

               out.close();                        

          }catch(Exception e){

               e.printStackTrace();

          }

     }

Regards,

Praveen

Former Member
0 Kudos

David,

Please find attached the relevant screenshots

....

Mutti

former_member229036
Participant
0 Kudos

mutti

your udf did not work..

could you let me know your email address in order to send source xml ?

thank you

former_member229036
Participant
0 Kudos

Hi praveen,  Mutti KK Rao;

the following is another invalid char,

like 'L' char was not written on java source

please help me  it has been annoyed.

thank you in advance.

nitindeshpande
Active Contributor
0 Kudos

Hi David,

As Inaki, told this cannot be fixed using UDF, you need to go for Java mapping as the parsing of characters must happen before the data is converted into XML, while using graphical mapping already the data is converted into XML and hence it is displayed as invalid XML.

My suggestion would be to first download the Hex editor from the below link and paste your source XML and find out what is the Hex code of your special character.

Freeware Hex Editor XVI32

Once you have the hex code, you can use the below Java mapping -

public class SpecailCharacter extends AbstractTransformation{

  public void transform(TransformationInput input, TransformationOutput output)

  throws StreamTransformationException {

  try {

  InputStream ins = input.getInputPayload().getInputStream();

  InputStreamReader isr = new InputStreamReader(ins);

  BufferedReader br = new BufferedReader(isr);

  StringBuilder payloadtemp =new StringBuilder();

  String payload ="";

  String s1;

s1 = br.readLine();

  while (s1!= null)

  {

  String s="";

  for(int i=0;i<s1.length();++i)

  {

    if(s1.charAt(i)!=your special character Hex Code)

    

         s=s+s1.charAt(i);

     }

  payloadtemp.append(s);

  }

  payload = payloadtemp.toString();

  output.getOutputPayload().getOutputStream().write(payload.getBytes());

  }

  catch (IOException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  }

  }

Please let me know if you face any difficulty in this.

Regards,

Nitin

former_member229036
Participant
0 Kudos

Hi Nitin Deshpande;

which one is hex code for my special character?   003 or &#03 ?

if i type like  if(s1.charAt(i)!='003',   eclipse error is invalid character constant.

only able to type one character.

if(s1.charAt(i)!=your special character Hex Code)

   

         s=s+s1.charAt(i);

     }

Regards.

David

nitindeshpande
Active Contributor
0 Kudos

Hello David,

Sorry for late reply, i was off work for few days. 03 is your hex code.

And in your case you need to mention your if loop as below -

if (s1.charAt(i)!=0x03)

Please let me know if you need any more help on this.

Regards,

Nitin Deshpande

Former Member
0 Kudos

Hi David,

We had a similar situation and I had to use two java mappings as below

1.replace special characters - convert escape character to xml character

2.Actual graphical mapping - your mapping goes here

3.reverse the special characters - convert it back to escape character

See attached the source code.

/**

*

*/

package xyz;

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

import java.io.*;

import java.text.*;

import java.util.*;

import com.sap.aii.af.lib.trace.*;

/**

* Mapping to convert xml escape character "&" to string characters understandable by PI

*/

public class replaceEscapeCharacters extends AbstractTransformation {

  public void transform(TransformationInput arg0, TransformationOutput arg1) 

    throws StreamTransformationException { 

String inputPayload = convertInputStreamToString(arg0.getInputPayload() 

         .getInputStream()); 

String outputPayload = ""; 

if (inputPayload.contains("&")) {

  inputPayload = inputPayload.replaceAll("&","&amp;");

}

outputPayload = inputPayload; 

try { 

    arg1.getOutputPayload().getOutputStream().write( 

              outputPayload.getBytes("UTF-8")); 

  }

catch (Exception exception1)

  { 

  getTrace().addWarning("Exception caught in Transform: " + exception1.toString());

  }

}

    

 

public String convertInputStreamToString(InputStream in) { 

StringBuffer sb = new StringBuffer(); 

try { 

    InputStreamReader isr = new InputStreamReader(in); 

    Reader reader = new BufferedReader(isr); 

    int ch; 

    while ((ch = in.read()) > -1) { 

         sb.append((char) ch); 

    } 

    reader.close(); 

} catch (Exception exception)

  { 

    getTrace().addWarning("Exception caught in convertInputStreamToString: " + exception.toString()); 

  } 

return sb.toString(); 

}  

}

package xyz;

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

import java.io.*;

import java.text.*;

import java.util.*;

import com.sap.aii.af.lib.trace.*;

/**

* Mapping to convert xml escape representation "&amp;" back to normal character "&"

**/

public class reverseEscape extends AbstractTransformation {

  public void transform(TransformationInput arg0, TransformationOutput arg1) 

    throws StreamTransformationException { 

getTrace().addInfo("JAVA Mapping replace function initiated"); 

String inputPayload = convertInputStreamToString(arg0.getInputPayload() 

         .getInputStream()); 

String outputPayload = ""; 

if (inputPayload.contains("&amp;")) {

inputPayload = inputPayload.replaceAll("&amp;","&"); 

}

outputPayload = inputPayload; 

try { 

    arg1.getOutputPayload().getOutputStream().write( 

              outputPayload.getBytes("UTF-8")); 

  }

catch (Exception exception1)

  { 

    getTrace().addWarning( 

            "Exception caught in Transform: " + exception1.toString()); 

  }

}

public String convertInputStreamToString(InputStream in) { 

StringBuffer sb = new StringBuffer(); 

try { 

    InputStreamReader isr = new InputStreamReader(in); 

    Reader reader = new BufferedReader(isr); 

    int ch; 

    while ((ch = in.read()) > -1) { 

         sb.append((char) ch); 

    } 

    reader.close(); 

} catch (Exception exception)

  { 

getTrace().addWarning("Exception caught in convertInputStreamToString: " + exception.toString()); 

  } 

return sb.toString(); 

}  

}