cancel
Showing results for 
Search instead for 
Did you mean: 

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

Former Member
0 Kudos

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.           
  16.             outputstream.write(inputContent.getBytes()); 
  17.         } catch (Exception exception) { 
  18.             getTrace().addDebugMessage(exception.getMessage()); 
  19.             throw new StreamTransformationException(exception.toString()); 
  20.         } 
  21.     } 
  22. }

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

Thanks,

Tanuja

Accepted Solutions (1)

Accepted Solutions (1)

stefan_grube
Active Contributor
0 Kudos

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.

Answers (2)

Answers (2)

Former Member
0 Kudos

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>

anupam_ghosh2
Active Contributor
0 Kudos

Hi Tanuja,

     what will be the output for this input ??

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



Regards

Anupam

anupam_ghosh2
Active Contributor
0 Kudos

Hi Tanuja,

                Please provide exact input xml payload during runtime.

Regards

Anupam