cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Encoding version replacment in xml file

former_member277492
Participant
7,073

Hello Experts,

I have got a requirement where Incoming xml with encoding version 'UTF-8' as below and output file is expected with 'ISO-8859-1' tag.

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

Output: <?xml version="1.0" encoding="ISO-8859-1"?> (Required)

Sample file structure below;

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

<EMPLEADOS>

<EMPLEADO NUMERO="100000676" TIPO="A">

<PROCESO PAC="000000" PERIODO="12345" TT="MN"/>

<SECCION ID="ABCD">

<CAMPO1 FECHA="" ID="PTP_POCATP" SEC="">0.97</CAMPO1>

<CAMPO2 FECHA="" ID="PTP_SPPOC105" SEC="">39.0</CAMPO2>

</SECCION7>

</EMPLEADO>

</EMPLEADOS>.

Do we have any groovy to fulfil this requirement. Please suggest.

Thanks in advance,

Best Regards,

Sree

View Entire Topic
Sriprasadsbhat
Active Contributor

Hello Sreekar,

Below should help you.I have tried to replicate your scenario ( without receiving system ).

Step 1:

Add input XML

<?xml version='1.0' encoding='UTF-8'?>
<Root>
	<Record>
		<CustomerName>üzüm bağları</CustomerName>
		<CustomerID>C1</CustomerID>
		<OrderNo>Ord1</OrderNo>
		<OrderDesc>Headset</OrderDesc>
		<OrderDate>15-11-2020</OrderDate>		
	</Record>
	<Record>
		<CustomerName>XYS Corp</CustomerName>
		<CustomerID>C2</CustomerID>
		<OrderNo>Ord12</OrderNo>
		<OrderDesc>HardDisc</OrderDesc>
		<OrderDate>11-11-2020</OrderDate>
	</Record>
	<Record>
		<CustomerName>üzüm bağları</CustomerName>
		<CustomerID>C3</CustomerID>
		<OrderNo>Ord22</OrderNo>
		<OrderDesc>Mouse</OrderDesc>
		<OrderDate>12-11-2020</OrderDate>
	</Record>
</Root>

Step 2:

Add below script if you want to convert encoding of input data ( content )

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
       //Get Body 
       def body = message.getBody();       
       def map = message.getProperties();
	  
       def op=new String(body.getBytes("UTF-8"), "ISO-8859-1");
       message.setBody(op);      
       message.setProperty("CamelCharsetName", "ISO-8859-1");      
       return message;    
}

Step 3:

Add XSLT mapping to remove XML declaration tag (which contains UTF-8)

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" omit-xml-declaration="yes" />
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Step 4:

Add content modifier to set the XML declaration tag you are looking for.

<?xml version="1.0" encoding="ISO-8859-1"?>
${in.body}

Regards,

Sriprasad Shivaram Bhat

former_member105769
Participant
0 Likes

Hi

The code in Step 2 gives an error:

Error Details

java.lang.NoSuchMethodException: No signature of method: org.apache.camel.component.cxf.converter.CachedCxfPayload.getBytes() is applicable for argument types: (java.lang.String) values: [UTF-8] Possible solutions: getAt(java.lang.String), getClass(), getBody()