on 10-29-2008 2:18 PM
Hi all
I am using Java mapping to replace a string with another string in my outputed xml file. the code i am using is
package xi_test_package;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.sap.aii.mapping.api.StreamTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
public class Xi_test_class implements StreamTransformation {
private Map param = null;
public void setParameter (Map param) {
this.param = param;
if (param == null) {
this.param = new HashMap();
}
}
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("ns0:");
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();
}
}
}
I picked this code from a different blog,This code is basically replacing "ns0" with "" . I am now required to tweak it so that the logic is like this .
replace all "ns0:" with "" and replace all "ns1:" with "" and replace all "ns2:" with "com:" and replace all occurenaces except the first one of "ns4" with "" and all occureance except the first one of "ns5" with "main"
How can i tweak the code to do all of these replacements in a single go
your expert java advice is well appricited.
Thank you
Try this.. I have just removed few lines related to pattern and used another piece of code here..
let me know if any issues..
>
> BufferedWriter out = new BufferedWriter(new OutputStreamWriter(outStream));
>
> >
> String aLine = null;
> int count = 0;
> while((aLine = in.readLine()) != null) {
>
>
if ( count > 0 )
> out.write( aLine.replaceAll( "ns0:","" ).replaceAll("ns1:","").replaceAll("ns2:","com:").replaceAll("ns4","") );
else
out.write( aLine.replaceAll( "ns0:","" ).replaceAll("ns1:","").replaceAll("ns2:","com:") );
>
> out.newLine();
> if ( aLine.indexOf( "ns4") > 0 ) count++;
> }
>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
hi,
I know you have got your result using java mapping but actually for your information in your case java mapping is not required.
This can be easily obtained using a already existing adapter module XMLAnonymizerBean.
you can refer to:
http://help.sap.com/saphelp_nw04/helpdata/en/2e/bf37423cf7ab04e10000000a1550b0/frameset.htm
regards,
ujjwal kumar
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ujjwal
I tried the adapter module to remove the prefixes, But the results are not exactly how i wanted , the adapter module is pretty inflexible , you can only specify valid namespaces and prefixes in it. Where as XI is creating a namespace decleration for each new XML node which is not necessary. If i used adapter module then it either deletes all of the namespaces or keeps all of them. So I chose to go with java mapping , I also tried XSLT but that proved complicated too. Thanks for your advice anyway.
i would take this approach;
convert the input stream to a string say input.
use the replace all in succession to achieve your result.
String output = input.replaceAll("<source string1>", <"target string1">);
String output = input.replaceAll("<source string2>", <"target string2">);
String output = input.replaceAll("<source string3>", <"target string3">);
assign output to your outputstream.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
81 | |
10 | |
10 | |
9 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.