on 08-07-2014 5:49 AM
Hi
in SAP PI, we got the error Character reference "" 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.
Hi,
Please provide your source xml.
BR,
Anirban
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
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 = "";
// i do not know
????????
}
in.close();
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
Regards;
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 = "";
// i do not know how to remove spc1 by java code
// ????????
}
in.close();
out.close();
}catch(Exception e){
e.printStackTrace();
}
Regards;
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
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.
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
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("&","&");
}
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 "&" 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("&")) {
inputPayload = inputPayload.replaceAll("&","&");
}
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();
}
}
User | Count |
---|---|
88 | |
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.