Skip to Content
0
Jun 24, 2015 at 10:40 AM

Need to remove XML indent from Target XML

778 Views

Hi All,

I came again with strange issues.

I need to remove xml Indent (<?xml version="1.0" encoding="UTF-8"?>) for this I tried with Java and XSLT mapping both able to remove this indent.

But here is the catch , I can see there is no indent in Operation Mapping but when file is getting created indent is there.

Can anyone help me on the same.

Note :- I am using SAP SFTP adapter ( SAP PI 7.11)

Please find the code below , Both Code are working fine .. šŸ˜”

XSLT :-


<xsl:stylesheet version="1.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>

Java :-

public class NameSpaceReplace extends AbstractTransformation {

InputStream inputStream = null;

OutputStream outputStream = null;

/**

* 1. Replace Namespaces.

*/

@Override

public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException {

try {

//Getting input stream and output stream.

inputStream = transformationInput.getInputPayload().getInputStream();

outputStream = transformationOutput.getOutputPayload().getOutputStream();

//Reading input stream content into a String.

byte[] b = new byte[inputStream.available()];

inputStream.read(b);

String strContent = new String(b);

// 1. Replace Namespaces.

strContent = strContent.replace('?','D');

strContent = strContent.replaceAll("<Dxml version=\"","");

strContent = strContent.replaceAll("1.0\"","");

strContent = strContent.replaceAll("encoding=\"","");

strContent = strContent.replaceAll("UTF-8\"","");

strContent = strContent.replaceAll("D>","");

strContent = strContent.trim();

//Write output to outputStream.

outputStream.write(strContent.getBytes());

} catch (IOException ex) {

getTrace().addDebugMessage(ex.getMessage());

throw new StreamTransformationException("Can not read input stream.", ex);

} finally {

try {

inputStream.close();

outputStream.close();

} catch (IOException ex) {

getTrace().addDebugMessage(ex.getMessage());

throw new StreamTransformationException("Can not close input stream or output stream.", ex);

}

}

}

}

Warm Regards,

Rudra