on 06-29-2012 3:12 AM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
86 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.