Skip to Content
author's profile photo Former Member
Former Member

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

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*;
  3. import*;
  4. public class WellformedXML_JavaMapping extends AbstractTransformation {
  5. @Override
  6. public void 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 = new byte[inputstream.available()];
  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. outputstream.write(inputContent.getBytes());
  16. } catch (Exception exception) {
  17. getTrace().addDebugMessage(exception.getMessage());
  18. throw new StreamTransformationException(exception.toString());
  19. }
  20. }
  21. }

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



Add a comment
10|10000 characters needed characters exceeded

Related questions

3 Answers

  • Best Answer
    Posted on Aug 24, 2016 at 05:10 AM

    When you replace all < and > in the whole XML, you also replace regular tag delimiters and make your XML corrupt. As your incoming XML is not valid, I would recommend that the issue would be fixed at the sender side. The sender must provide valid XML.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Aug 23, 2016 at 02:54 PM

    Hi Tanuja,

    Please provide exact input xml payload during runtime.



    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Aug 23, 2016 at 03:03 PM

    Hi Anupam,

    We have removed CDATA before this java mapping. It's failing at receiver end due to this special characters.

    I have issue with only this field <MessageText>Special characters Test &amp;apos; &amp;apos; # ~ + = </MessageText> .

    <?xml version="1.0" encoding="UTF-8"?>











    <AddressLine1>51 BERTRAM ROAD</AddressLine1>









    <CountryName>United Kingdom</CountryName>















    <MessageText>Special characters Test &amp;apos; &amp;apos; # ~ + = </MessageText>
































    <Colour>No Colour</Colour>


    <ItemDescription>CC CANDLE JUNIPER &amp; GINGER XMAS 3</ItemDescription>


    <ShortDescription>CC CANDLE JUNIPER &amp; GINGER XMAS 3</ShortDescription>

























    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.