I have requirement to replace invalid xml characters in xml body text.
& with &
< with >
> with <
" with "
' with '
for eg: <SizeCode>white <> black & green ' "</SizeCode>
Current result:
<SizeCode>white >< black & green ' "</SizeCode<
Expected result:
<SizeCode>white >< black & green ' "</SizeCode>
I am using below java mapping, But It's replacing XML tags as well.
- package com.javaMapping;
- import java.io.*;
- import com.sap.aii.mapping.api.*;
- publicclass WellformedXML_JavaMapping extends AbstractTransformation {
- @Override
- publicvoid transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException {
- try {
- InputStream inputstream = transformationInput.getInputPayload().getInputStream();
- OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();
- // a) Copy Input content to String
- byte[] b = newbyte[inputstream.available()];
- inputstream.read(b);
- String inputContent = new String(b);
- inputContent = inputContent.replaceAll("&", "____someTEXT_____").replaceAll("&", "&").replaceAll("____someTEXT_____", "&").replaceAll(">", "____someTEXT_____").replaceAll(">", ">").replaceAll("____someTEXT_____", ">").replaceAll("<", "____someTEXT_____").replaceAll(<", "<").replaceAll("____someTEXT_____", "<").replaceAll(""", "____someTEXT_____").replaceAll("/"", """).replaceAll("____someTEXT_____", """).replaceAll("'", "____someTEXT_____").replaceAll("'", "'").replaceAll("____someTEXT_____", "'");
- outputstream.write(inputContent.getBytes());
- } catch (Exception exception) {
- getTrace().addDebugMessage(exception.getMessage());
- thrownew StreamTransformationException(exception.toString());
- }
- }
- }
Please help me to correct this Java code or Provide me some working code
Thanks,
Tanuja