cancel
Showing results for 
Search instead for 
Did you mean: 

Replace special characters < > in the data content of xml document.

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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 = { "]", "&amp;", "&lt;", "&gt;", "&quot;", ",", "#", "\\$", "_", "@", "\\[", "\\)", "\\("};
               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());
         
     }
}


anupam_ghosh2
Active Contributor
0 Kudos

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 = { "]", "&amp;", "&lt;", "&gt;", "&quot;", ",", "#", "\\$", "_", "@", "\\[", "\\)", "\\("};"

Then this makes me think that XML was actually containing valid XML characters (such as &amp instead of &). With Little modification ' s code could have also worked.

Regards

Anupam

former_member188961
Participant
0 Kudos

  Yes.. .. my code will work for the above requirement. Just need to change the

below line of code according to his requirement.

  inputPayload = inputPayload.replaceAll("& # $ _ @","< >"); 


Thanks,

Srikanth

Former Member
0 Kudos

Hi Ghosh, Srikanth

yes you are right, initially i was unaware that the escape symbols are also coming with the special characters.

thats why posted my query here, any ways thanks for your help

Navneet.

anupam_ghosh2
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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

Answers (4)

Answers (4)

praveen_sutra
Active Contributor
0 Kudos

Hi Navneet,

Hope this document helps you.

http://scn.sap.com/docs/DOC-46151

thanks and regards,

Praveen T

iaki_vila
Active Contributor
0 Kudos

Hi Navneet,

As 

http://www.w3schools.com/tags/ref_entities.asp

Regards.

rodrigoalejandro_pertierr
Active Contributor
0 Kudos

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

former_member188961
Participant
0 Kudos

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

Former Member
0 Kudos

Hi Srikanth,

Thanks for your reply, but this code will also replaces < > from the xml tags which will generate wrong xml document.

I just want to check the data content of the xml document and replace these special character.

Thanks

Navneet

Former Member
0 Kudos

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. &amp; for &, for example.

Regards,

Jörg

anupam_ghosh2
Active Contributor
0 Kudos

Hi Navneet,

                    Is this problem resolved ? Or still you are facing an issue with this.

Regards

Anupam