cancel
Showing results for 
Search instead for 
Did you mean: 

Encoding version replacment in xml file

former_member277492
Participant

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

Accepted Solutions (1)

Accepted Solutions (1)

Sriprasadsbhat
Active Contributor
0 Kudos

Hello Sreekar,

Please close the thread if above answers your query by accepting correct answer.

Regards,

Sriprasad Shivaram Bhat

Answers (15)

Answers (15)

Sriprasadsbhat
Active Contributor

Hello Sreekar,

Before you send the data to Target system add content modifier and set the property with below parameters which changes the Encoding.

Action       Name                    Type                  Value
Create       CamelCharsetName        Constant              ISO-8859-1
        

Regards,

Sriprasad Shivaram Bhat

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 Kudos

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()
MortenWittrock
Active Contributor

Hi Sreekar

You can't just change the value of the encoding attribute in the XML prolog (the <?xml .... ?> part at the beginning of your document). You need to change the actual encoding of the document and then reflect that in the encoding attribute. The answer from sriprasadshivaramabhat shows you how.

Regards,

Morten

former_member277492
Participant
0 Kudos

Hello Shivaram,

I have accepted the answer and closed the thread.

Thanks,

Best Regards,

Sree

former_member277492
Participant
0 Kudos

Hello Shivaram,

Thank you for the prompt response.

I have Implemented recommended steps and achieved the requirement as output xml with ISO encoding tag.

I have used XSLT and set the xml declaration tag before target.

Thank you once again for your time and help!!

Best Regards,

Sree

former_member277492
Participant
0 Kudos

Hello Shivaram,

Any clue about above issue.

Thanks in advance,

Sree

former_member277492
Participant
0 Kudos

Hello Shivaram,

Any clue on above mentioned topic please.

Best Regards,

Sree

former_member277492
Participant
0 Kudos

Hello Shivaram,

I am using SFTP adapter and output structure is xml.

Thanks!!

Sree

Sriprasadsbhat
Active Contributor
0 Kudos

Hello Sreekar,

Quick question which target adapter you are using and which format it should be.

Regards,

Sriprasad Shivaram Bhat

former_member277492
Participant
0 Kudos

Hello Vijendra and Team,

Please could you help me out on this.

Best Regards,

Sree

former_member277492
Participant
0 Kudos

Hello Vijendra,

Thanks for the response.

I have tried the above provided approach. After Message Mapping I have used Content Modifier, set the below property and then i used the Groovy script before target.

Please let me know anything wrong am I doing or correct me any gap in my understanding

Thanks and Regards,

Sree

vijender_p
Active Participant
0 Kudos

Hi sreekar ,

As explained above please store the value as a property" CamelCharsetName"

After that please use the below code in the mapping then you will achieve the data as output:

import com.sap.it.api.mapping.*;
import com.sap.it.api.mapping.MappingContext;
def String getProperty(String CamelCharsetName, MappingContext context) {
    def propValue= context.getProperty(property_name);
    return propValue;
}

Where You need to pass constant as " CamelCharsetName" to the Script and Map the target field.

Regards,
Vijender

former_member277492
Participant
0 Kudos

Hello Shivaram and Team,

Please could you suggest on above request on encoding version change.

Best Regards,

Sree

former_member277492
Participant
0 Kudos

Hello Shivaram and Morten,

Thanks for your response.

I have configured content modifier after mapping step and configured the shared details and still in output payload i am getting UTF-8 only.

Enclsoing the screenshots for your kind perusal.

Best Regards,

Sree

former_member277492
Participant
0 Kudos

Hello Shivaram and Morten,

Thanks for your response.

I have configured content modifier after mapping step and configured the shared details and still in output payload i am getting UTF-8 only.

Kindly advise,

Best Regards,

Sree