cancel
Showing results for 
Search instead for 
Did you mean: 

Parsing Special characters in XML using JAVA Code in SAP PI

Former Member
0 Kudos

Hi All,

I have an issue parsing 5 special characters ie ("Á,Ï,Í,Ð,Ÿ) beause of these 5 characters the xml is unable to parse them

It works fine with graphical mapping "replace string" function...but I want to go with Java mapping, UDF

Can someone guide me if this code is correct

public class UTFISO implements StreamTransformation {

private final String l = "Á,Ï,Í,Ð,Ÿ";

private final String u ="A,I,I,D,Y";

public void execute(InputStream in, OutputStream out) {

    

  try {

   DataInputStream stdin = new DataInputStream(in);

   int length = getLengthFromStream(stdin);

   stdin.reset();

   byte[] buffer = getBytesFromStream(stdin);

   String str = new String(buffer, u);  

  

   str = str.replaceAll(u, l);

   out.write(str.getBytes(l));

   out.close();

      

  } catch (IOException e) {

    }

}

Also tried with code

String s = "";

String a = "";

s = a.replace("Á" , "A");

return s;

but this returns a empty or what ever value i give it to string ,but I need at runtime to capture the incomming value and if any special charecter is found replace it

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

Before introducing workarounds, have you checked why these wrong characters are in the payload ? Probably the sender system sent not well-formed data. Ask them to do it right.

Because if you go with the solution above, you probably will find yourself adding more and more special replacements in future, because simply the sending system sends wrong encoding.

=> you are fixing issues which are caused by the sender side and should be solved there.

CSY

anupam_ghosh2
Active Contributor
0 Kudos

Hi Sudheer,

                    You wrote

"Also tried with code

String s = "";

String a = "";

s = a.replace("Á" , "A");

return s;

but this returns a empty or what ever value i give it to string ,but I need at runtime to capture the incomming value and if any special charecter is found replace it"

Please check my respone in this  thread: http://scn.sap.com/thread/3199934

Specify the ASCII code of the special character you want to replace instead of character themselves.

Then above code will work.

Regards

Anupam

iaki_vila
Active Contributor
0 Kudos

HI suddher,

You can try with the next code:

import java.text.Normalizer;
import java.util.regex.Pattern;

public String deAccent(String str) {
   
String nfdNormalizedString = Normalizer.normalize(str, Normalizer.Form.NFD);
   
Pattern pattern = Pattern.compile("\\p{InCombiningDiacriticalMarks}+");
   
return pattern.matcher(nfdNormalizedString).replaceAll("");
}

Not all substitutions are allowed, may be some of you accented characters could be transformed.

Regards

markangelo_dihiansan
Active Contributor
0 Kudos

Hello,

Before using any find/replace function, you should try to convert the original xml encoding to utf-8 e.g

              

byte[] latin1 = yourInputConvertedtoString.getBytes("ISO-8859-1");

byte[] utf8 = new String(latin1, "ISO-8859-1").getBytes("UTF-8");

String xml = new String(utf8, "UTF-8");

*Your find/replace logic goes here using the String xml

Hope this helps,

Mark