Quantcast
Channel: SCN : All Content - All Communities
Viewing all articles
Browse latest Browse all 3323

Replace invalid xml character with their escape sequence in xml body text

$
0
0

I have requirement to replace invalid xml characters in xml body text.

 

& with &

< with &gt;

> with &lt;

" with &quot;

' with &apos;

 

for eg: <SizeCode>white <> black & green ' "</SizeCode>

 

Current result:

&lt;SizeCode&gt;white &gt;&lt; black &amp; green &apos; &quot;&lt;/SizeCode&lt;

 

Expected result:

 

<SizeCode>white &gt;&lt; black &amp; green &apos; &quot;</SizeCode>

 

 

I am using below java mapping, But It's replacing XML tags as well.

 

  1. package com.javaMapping; 
  2. import java.io.*; 
  3. import com.sap.aii.mapping.api.*; 
  4. publicclass WellformedXML_JavaMapping extends AbstractTransformation { 
  5.     @Override 
  6.     publicvoid transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException { 
  7.         try
  8.             InputStream inputstream = transformationInput.getInputPayload().getInputStream(); 
  9.             OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream(); 
  10.             // a) Copy Input content to String 
  11.             byte[] b = newbyte[inputstream.available()]; 
  12.             inputstream.read(b); 
  13.             String inputContent = new String(b); 
  14.      inputContent = inputContent.replaceAll("&amp;", "____someTEXT_____").replaceAll("&", "&amp;").replaceAll("____someTEXT_____", "&amp;").replaceAll("&gt;", "____someTEXT_____").replaceAll(">", "&gt;").replaceAll("____someTEXT_____", "&gt;").replaceAll("&lt;", "____someTEXT_____").replaceAll(<", "&lt;").replaceAll("____someTEXT_____", "&lt;").replaceAll("&quot;", "____someTEXT_____").replaceAll("/"", "&quot;").replaceAll("____someTEXT_____", "&quot;").replaceAll("&apos;", "____someTEXT_____").replaceAll("'", "&apos;").replaceAll("____someTEXT_____", "&apos;"); 
  15.           
  16.             outputstream.write(inputContent.getBytes()); 
  17.         } catch (Exception exception) { 
  18.             getTrace().addDebugMessage(exception.getMessage()); 
  19.             thrownew StreamTransformationException(exception.toString()); 
  20.         } 
  21.     } 
  22. }

 

Please help me to correct this Java code or Provide me some working code

 

Thanks,

Tanuja


Viewing all articles
Browse latest Browse all 3323


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>