Quantcast
Channel: Process Integration (PI) & SOA Middleware
Viewing all articles
Browse latest Browse all 741

Not well-formed XML - & issue

$
0
0

Here is some data.

Jack & Jill went up < the hill.

A program which comply with W3C standard will produce below XML.

<root>  <who>Jack &amp; Jill</who>  <where>went up &lt; the hill.</where></root>

Why & and < are represented as &amp; and &lt;?

     If they are not represented that way (escaped), then it is difficult (if not impossible) to write a generic program logic to parse the XML or convert XML back to text. W3C has reserved < > & ‘ “ as Predefined Entities. These characters needs to be escaped in a well-formed XML.

 

Let’s say a program does not obey this rule. It produces

<root>  <who>Jack & Jill</who>  <where>went up < the hill.</where></root>

     This is not a well-formed XML. It will fail in message mapping with error “Character reference "&xxxxx" is an invalid XML character”. Common issue on SCN, “handling special character in XML in message mapping”. Graphical message mapping, XSLT, DOM, SAX can’t handle this XML.

 

It is highly recommended to request the sender system, to fix the code and generate well-formed XML.

 

     In most cases, & is not escaped. If < is not escaped, it is very default to handle. Java mapping can be used to produce well-formed XML. Note: Generic program logic is difficult, as & is Predefined Entity. Tailor below solution as needed.

 

Solution:-

Download the payload and save it with .txt extension. Open the payload with foxe or notepad++. Check the element where mapping threw error message. Determine the XML is not well-formed.

Implement below Java mapping before message mapping. i.e., select below Java mapping and then your message mapping in operational mapping.

package com.javaMapping;
import java.io.*;
import com.sap.aii.mapping.api.*;
public class WellformedXML_JavaMapping extends AbstractTransformation {    @Override    public void 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 = new byte[inputstream.available()];            inputstream.read(b);            String inputContent = new String(b);            // b) Replace all & with &amp; Use this when every & in input is not escaped as &amp;            inputContent = inputContent.replaceAll("&", "&amp;");            // Use this when a few & are escaped as &amp; and a few & are not escaped. This logic will avoid &amp; to become &amp;amp;            //inputContent = inputContent.replaceAll("&amp;", "____someTEXT_____").replaceAll("&", "&amp;").replaceAll("____someTEXT_____", "&amp;");                      outputstream.write(inputContent.getBytes());        } catch (Exception exception) {            getTrace().addDebugMessage(exception.getMessage());            throw new StreamTransformationException(exception.toString());        }    }
}

Viewing all articles
Browse latest Browse all 741

Trending Articles



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