cancel
Showing results for 
Search instead for 
Did you mean: 

XML to single element: Attn: Henrique

Former Member
0 Kudos

Hi Henrique,

Could you pls tell me in detail about how to achieve One whole xml into one string element. I am new to all this and have not used xslt or java mapping before.

Regards,

Ashish

Accepted Solutions (0)

Answers (4)

Answers (4)

ankit_mishra09
Participant
0 Kudos

Hi

Were you able to get the payload in a single field using UDF? Is it possible?

former_member189440
Participant
0 Kudos

Hi ,

u can use XSLT mapping for inserting the complete input xml file in to string on output.

This is the sample code:

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

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<input_tag>

<xsl:text disable-output-escaping="yes"><![CDATA[><![CDATA[]]></xsl:text>

<xsl:copy-of select="output_tag/string_name"/>

<xsl:text disable-output-escaping="yes"><![CDATA[]]]]></xsl:text>

<xsl:text disable-output-escaping="yes"><![CDATA[>]]></xsl:text>

</input_tag>

</xsl:template>

</xsl:stylesheet>.

or u can use java mapping also

below is the sample code:

//package com.xi.javamapping;

import java.io.DataInputStream;

//import java.io.File;

//import java.io.FileInputStream;

//import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.Map;

import com.sap.aii.mapping.api.StreamTransformation;

import com.sap.aii.mapping.api.StreamTransformationException;

public class XMLToString implements StreamTransformation {

public void setParameter(Map arg0) {}

public void execute(InputStream arg0, OutputStream arg1)

throws StreamTransformationException {

try {

DataInputStream dataInputStream = new DataInputStream(arg0);

StringBuffer sbr = new StringBuffer();

String str = null;

while ((str = dataInputStream.readUTF()) != null) {

String str1 = str.trim();

sbr.append(str1);

String string = new String(sbr);

byte[] b = string.getBytes();

arg1.write(b);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

bhavesh_kantilal
Active Contributor
0 Kudos

Use XSL as shown by Prakash or use Java mapping with the following code,

BufferedReader inp = new BufferedReader(new InputStreamReader(in));
StringBuffer buffer = new StringBuffer();
String line="";
while ((line = inp.readLine()) != null) {
buffer.append(line);
}
String  source=buffer.toString();

Regards,

Bhavesh

Former Member
0 Kudos

Thanks a lot. I am decided to go for java user defined function. In the mapping editor, I tried to create the user defined function. But facing some errors when executed. What should I give the label name and argument according to Bhavesh's java code. Do I need to import any?

Here is the java code.

BufferedReader inp = new BufferedReader(new InputStreamReader(in));

StringBuffer buffer = new StringBuffer();

String line="";

while ((line = inp.readLine()) != null) {

buffer.append(line);

}

String source=buffer.toString();

Thanks

Deno

bhavesh_kantilal
Active Contributor
0 Kudos

Deno,

<i>I am decided to go for<b> java user defined function</i></b>

You need to use the code in a Java Mapping Class and not in an Userdefined function.

Regards,

Bhavesh

henrique_pinto
Active Contributor
0 Kudos

Heya,

sorry for the 5 months delay in answering that. But since it's still in "not answered" status, I thought "hey, why not?". 🐵

For XML into a tag <b>as a string</b>, use the following XSLT mapping:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://www.yourmessage.com">
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
	<xsl:template match="/">
		<m:RequestMessage>
			<m:SimpleTag>
				<xsl:text disable-output-escaping="yes"><![CDATA[<![CDATA[
				
				]]
				>
			
		
	
]]>

To retrieve the response from a XML into a tag <b>as a string</b>, use the following code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://www.yourmessage.com">
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="no"/>
	<xsl:template match="/m:ResponseMessage/m:SimpleTag">
		<xsl:value-of select="." disable-output-escaping="yes"/>
	</xsl:template>
</xsl:stylesheet>

Just a hint: in the case of the response XML String having the xml initial tag (<?xml version="1.0" encoding="UTF-8"?> or something like that) in it, you may find some errors due to duplication of that tag. In order to prevent that, use the output method="text" in the retrieving XSLT (second code).

Hope it helps.

Regards,

Henrique.

former_member206604
Active Contributor
0 Kudos

Hi,

You can use this XSLT for passing it to a single element

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<Target>
<singleTag>
 <xsl:copy-of select="." />
</SingleTag>
</Target>
</xsl:template>
</xsl:stylesheet>

Assumend target structure is

Target

- SingelTag

Thanks,

Prakash