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 java.io.*;
  3. import com.sap.aii.mapping.api.*;
  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()];
  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. 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

Thanks,

Tanuja

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.

    Regards

    Anupam

    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"?>

    <test>

    <content>

    <data>

    <Deliveries>

    <Delivery>

    <DeliveryHeader>

    <ShipToParty>

    <Title>Mr</Title>

    <FirstName>er</FirstName>

    <LastName>sdghfd</LastName>

    <AddressLine1>51 BERTRAM ROAD</AddressLine1>

    <AddressLine2/>

    <AddressLine3/>

    <AddressLine4/>

    <AddressLine5/>

    <AddressLine6/>

    <City>ENFIELD</City>

    <PostCode>en11lr</PostCode>

    <County>MIDDX</County>

    <CountryName>United Kingdom</CountryName>

    <Country>GB</Country>

    <Email>abc@test.com</Email>

    <PartyID/>

    <PhoneNumber>+457456754</PhoneNumber>

    </ShipToParty>

    <DeliveryDate>2016-08-27T00:00:00+01:00</DeliveryDate>

    <DeliveryMethod>Saturday</DeliveryMethod>

    <DeliveryOption>SA</DeliveryOption>

    <DeliveryOptionDescription>Saturday</DeliveryOptionDescription>

    <DeliveryNumber>0</DeliveryNumber>

    <DeliveryCharge>15.95</DeliveryCharge>

    <Messages>

    <Message>

    <MessageType>GIFT</MessageType>

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

    </Message>

    </Messages>

    </DeliveryHeader>

    <OrderLines>

    <OrderLine>

    <OrderLineNumber/>

    <OrderLineType>GM</OrderLineType>

    <ItemID>000000000004126152</ItemID>

    <Charges>

    <Charge>

    <ChargeID>Shipping_Charge</ChargeID>

    <Description>Shipping</Description>

    <Amount>15.95</Amount>

    <ChargePerUnit>0.00</ChargePerUnit>

    <IsDiscount>N</IsDiscount>

    <isBillable>Y</isBillable>

    </Charge>

    </Charges>

    <Taxes>

    <Tax>

    <TaxID>Sales_Tax</TaxID>

    <Description>Sales_Tax</Description>

    <Amount>7.66000</Amount>

    </Tax>

    <Tax>

    <TaxID>Shipping_Tax</TaxID>

    <Description>Shipping_Tax</Description>

    <Amount>2.66000</Amount>

    </Tax>

    </Taxes>

    <SizeCode>NONE</SizeCode>

    <Colour>No Colour</Colour>

    <Brand>Cochine</Brand>

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

    <LongDescription/>

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

    <Quantity>1</Quantity>

    <UnitOfMeasure>EACH</UnitOfMeasure>

    <UnitPrice>45.95</UnitPrice>

    <TotalAmount>45.95</TotalAmount>

    <LineTax>0.00</LineTax>

    <LineDiscount>0.00</LineDiscount>

    <LineCharge>15.95</LineCharge>

    <ExtendedAmount>45.95</ExtendedAmount>

    <OriginalQty>0</OriginalQty>

    <ChangedQty>0</ChangedQty>

    <Messages>

    <Message>

    <MessageType/>

    <MessageText/>

    </Message>

    </Messages>

    <Status/>

    </OrderLine>

    </OrderLines>

    </Delivery>

    </Deliveries>

    </data>

    </content>

    </test>

    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.