on 10-07-2013 2:05 PM
Hi,
Can anyon please help me to write a Java code to replace special charaters( & # $ _ @ = [ < >) from the data content of an input xml file?
there is no transformation at all, i just have to pick the file and place it with all these special charaters removed from the xml file.
is there any better way to do it in PI?
Thanks
Navneet
Hi All,
Thanks for your help, the issue is resolved now.
The special characters were coming with escape symbol which were easily replaced using below java mapping code.
package com.kcc.citibank.inbound;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import com.sap.aii.mapping.api.AbstractTrace;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.DynamicConfigurationKey;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
public class REPLACESC implements StreamTransformation {
private AbstractTrace trace = null;
private Map param = null;
String inputFileName;
public void setParameter(Map param) {
this.param = param;
if (param == null) {
this.param = new HashMap();
}
}
public void execute(InputStream in, OutputStream out)
throws StreamTransformationException {
trace = (AbstractTrace) param.get(StreamTransformationConstants.MAPPING_TRACE);
trace.addWarning("JAVA Mapping Start");
/*this block of code will remove all the not allowed special characters from outbound xml*/
try
{
byte b[]=new byte[in.available()];
String encoding="UTF-8";
in.read(b);
String inputXML=new String(b);
String[] check = { "]", "&", "<", ">", """, ",", "#", "\\$", "_", "@", "\\[", "\\)", "\\("};
for (int i = 0; i < check.length; i++) {
inputXML=inputXML.replaceAll(check[i]," ");
}
out.write(inputXML.getBytes(encoding));
}
catch(Exception e)
{
throw new StreamTransformationException(e.getCause().toString());
}
/*block of code will fetch the file name from ASMA and add .xml extension */
try {
DynamicConfiguration conf = (DynamicConfiguration) param.get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey keyFileName = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File", "FileName");
inputFileName = conf.get(keyFileName);
conf.put(keyFileName,inputFileName+".xml");
}
catch(Throwable throwable) {
throwable.printStackTrace();
}
}
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
this.execute(arg0.getInputPayload().getInputStream(), arg1.getOutputPayload().getOutputStream());
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Navneet,
In your first post you wrote "Can anyone please help me to write a Java code to replace special charaters( & # $ _ @ = [ < >) from the data content of an input xml file?". But the characters you replaced are
"String[] check = { "]", "&", "<", ">", """, ",", "#", "\\$", "_", "@", "\\[", "\\)", "\\("};"
Then this makes me think that XML was actually containing valid XML characters (such as & instead of &). With Little modification srikanth kakani' s code could have also worked.
Regards
Anupam
Hi Navneet,
Could you think of any solution supposing your initial query becomes true. I mean to say that the payload values contains &,<,> etc and you need to remove them. I am trying thinking on the same, in case you have any ideas please share with the forum.
Thank you for posting your solution code. What about using replacestring standard function instead of java mapping?
Regards
Anupam
Message was edited by: Anupam Ghosh
hi Ghosh,
The only solution i can see is to parse the xml document first and then check each data element for special characters and then again build the xml with the output values. But it seems special characters like & < > without escape symbol will make an invalid xml and document builder may not even parse/read it.
We need to explore other possibilities for this requirement.
What about using replacestring standard function instead of java mapping?
I tried using replace string but it can replace only one symbol or string at a time and we have around 8-9 special characters to check for each element, and also the xml is too big as there are many elements in the xsd so it seems Java map was the better solution.
Thanks
Navneet
Hi Navneet,
Hope this document helps you.
http://scn.sap.com/docs/DOC-46151
thanks and regards,
Praveen T
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Navneet,
As Jörg Thiessmann said if you have a XML with special character without escaped symbol, it's an incorrect XML. Please talk who create that XML and tell them to create the XML file with the special character escaped or with the clausule CDATA.
There are so many special characters, check this link http://www.w3schools.com/tags/ref_entities.asp
Regards.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
insted of JAva Mapping i recommend you to create an Adapter Module and replace the strings, you can use the code in the response above.
also you can use an OS command to repalce the strings
take a look to this thread
http://scn.sap.com/thread/497163
Rgds
Rodrig
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Navneet,
you can try the below replace code by using Java mapping for your requirement.
public class replaceString extends AbstractTransformation {
public void transform(TransformationInput arg0, TransformationOutput arg1)
throws StreamTransformationException {
/
getTrace().addInfo("JAVA Mapping replace function intiated");
String inputPayload = convertInputStreamToString(arg0.getInputPayload()
.getInputStream());
String outputPayload = "";
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();
}
}
Hope this helps you!
Thanks,
Srikanth
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Navneet,
in my opinion an XML file with unmasked <, > etc. is not a legal input file, it is not valid. It shouldn't be allowed to provide this to PI.
If you need to transform it, it is probably better do use a DOM parser and go through all nodes,(the DOM parser would probably fail) take the node value You probably need to go through this tag by tag and substitute special characters by "". Or, even better, replace them with the correct escape string, i.e. & for &, for example.
Regards,
Jörg
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
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.