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

Converting string to XML

Former Member
0 Likes
2,251

Dear All,

I am using PI 7.1

I am trying to invoke a third party SOAP API which accepts request in the form of a string which has embedded xml and also return a string which has xml embedded in it.

To send the xml as string, I used the feature "Retun as XML" in mapping and it worked fine.

But the response which has xml embedded, needs to be converted back to XML message.

I understand that we can write parsers in java, ABAP and also do XSLT mapping.

Just wanted to know that is there any "out of the box" feature to do the same.

Please refrain from providing links to java mappings or xslt as I have gone through them already.

regards,

Piyush

Accepted Solutions (1)

Accepted Solutions (1)

Shabarish_Nair
Active Contributor
0 Likes

if the question is around out of the box feature, then the answer is NO

Answers (3)

Answers (3)

former_member200339
Participant
0 Likes

You can use DOM or SAX API for XML parsing. SAX is preferred as it is more memory intensive provided there is no need to modify the XML.

baskar_gopalakrishnan2
Active Contributor
0 Likes

>Just wanted to know that is there any "out of the box" feature to do the same

Answer is NO. I would use XSLT Mapping or Java mapping to achieve this.

former_member854360
Active Contributor
0 Likes

Hi ,

If your XMl is well formed then you can directly handle it in graphical Mapping .

If your string response contain xml declaration <?xml version="1.0" encoding="UTF-8"?>

then you can using UDF or java/xsl remove it. Then your output will be an well formed xml

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

<ns0:MT_Test_Receiver xmlns:ns0="http://testinterface.com">

<Item>

<ResonsefromSOAP>

test

</ResonsefromSOAP>

</Item>

</ns0:MT_Test_Receiver>

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

<ns0:MT_Test_Receiver xmlns:ns0="http://testinterface.com">

<Item>

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

<ResonsefromSOAP>

test

</ResonsefromSOAP>

</Item>

</ns0:MT_Test_Receiver>

Former Member
0 Likes

Hi Debashish,

My response is as below and I tried it already in graphical mapping but it doesnt work.

If you have trued this approach then please have a look at the response that I have and plz chek if it can be directly converted to xml structure.

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

- <!-- XML Validation Inbound Channel Response

-->

- <ns1:AuthenticateResponse xmlns:ns1="urn:Login" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">

<return xsi:type="xsd:string"><?xml version="1.0"?><MessageResponse sessionid=";jsessionid=FxZLTxtJnXJttpW6gMhJ06kyn00tfSgjt9ZwbzJ55CXGn92S8L6f!-444760742!1311845684753" status="SUCCESS" topicprefix="ND-DSK-117.CXL_HMEL_DB"><Entity name="REF_PERSON"><Property name="last_access_dt" value="20110728145937" type="DATE"/><Property name="modify_person_num" value="21" type="INTEGER"/><Property name="persontopic" value="350523952" type="STRING"/><Property name="login_result_ind" value="0" type="INTEGER"/><Property name="login_result_reason" value="" type="STRING"/><Property name="bypass_drafts" value="0" type="STRING"/><Property name="stl_check_swap_option_pricing_end" value="1" type="BOOLEAN"/><Property name="stl_update_provisional_cashflows" value="0" type="BOOLEAN"/><Property name="stl_autoinvoice_costs" value="1" type="BOOLEAN"/><Property name="stl_allow_payments_from_agents" value="0" type="BOOLEAN"/><Property name="stl_payment_allocation_at_invoice_detail_level" value="1" type="BOOLEAN"/><Property name="last_db_reloaded_on" value="" type="DATE"/><Property name="xl_mode_ind" value="0" type="INTEGER"/><Property name="Xchange_enabled" value="1" type="INTEGER"/><Property name="Xchange_server" value="XCHANGE" type="STRING"/><Property name="ipaddress" value="10.60.4.80" type="STRING"/><Property name="appname" value="CommodityXL" type="STRING"/><Property name="version" value="7.XL7.08.17E" type="STRING"/><Property name="econfirm.server.enabled" value="0" type="BOOLEAN"/></Entity></MessageResponse></return>

</ns1:AuthenticateResponse>

regards,

Piyush

Former Member
0 Likes

Hi Piyush,

The best approach, I would feel to handle it with Java Mapping.

Thanks,

anupam_ghosh2
Active Contributor
0 Likes

Hi Piyush,

All parsers will fail to parse the XML message you are getting. Since you are getting the following tag twice within the XMl


"<?xml version="1.0"?>" 

This tag can be present in an document only once in begining of the document. First you need to remove this tag from appearing in source.

Suppose you are able to do that then your XML structure will become something like this


<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
<ns1:AuthenticateResponse xmlns:ns1="urn:Login" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
<return xsi:type="xsd:string"><MessageResponse sessionid=";jsessionid=FxZLTxtJnXJttpW6gMhJ06kyn00tfSgjt9ZwbzJ55CXGn92S8L6f!-444760742!1311845684753" status="SUCCESS" topicprefix="ND-DSK-117.CXL_HMEL_DB"><Entity name="REF_PERSON"><Property name="last_access_dt" value="20110728145937" type="DATE"/><Property name="modify_person_num" value="21" type="INTEGER"/><Property name="persontopic" value="350523952" type="STRING"/><Property name="login_result_ind" value="0" type="INTEGER"/><Property name="login_result_reason" value="" type="STRING"/><Property name="bypass_drafts" value="0" type="STRING"/><Property name="stl_check_swap_option_pricing_end" value="1" type="BOOLEAN"/><Property name="stl_update_provisional_cashflows" value="0" type="BOOLEAN"/><Property name="stl_autoinvoice_costs" value="1" type="BOOLEAN"/><Property name="stl_allow_payments_from_agents" value="0" type="BOOLEAN"/><Property name="stl_payment_allocation_at_invoice_detail_level" value="1" type="BOOLEAN"/><Property name="last_db_reloaded_on" value="" type="DATE"/><Property name="xl_mode_ind" value="0" type="INTEGER"/><Property name="Xchange_enabled" value="1" type="INTEGER"/><Property name="Xchange_server" value="XCHANGE" type="STRING"/><Property name="ipaddress" value="10.60.4.80" type="STRING"/><Property name="appname" value="CommodityXL" type="STRING"/><Property name="version" value="7.XL7.08.17E" type="STRING"/><Property name="econfirm.server.enabled" value="0" type="BOOLEAN"/></Entity></MessageResponse></return> 
</ns1:AuthenticateResponse>

Then you can use the following java mapping code to remove the SOAP envelop.



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

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

public class RemoveSoapEnvelopSDN implements StreamTransformation{




public void execute(InputStream in, OutputStream out)
throws StreamTransformationException {


try
{
	DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
	DocumentBuilder builderel=factory.newDocumentBuilder();
	/*input document in form of XML*/
	Document docIn=builderel.parse(in);
	/*document after parsing*/
	Document docOut=builderel.newDocument();
	TransformerFactory tf=TransformerFactory.newInstance();
	Transformer transform=tf.newTransformer();
	Element root;
	Node p;
	
	
	NodeList l;
	int mm,n1;
	//if you need to include namespace use next two lines 
	//root=docOut.createElement("ns0:MessageResponse");
	//root.setAttribute("xmlns:ns0","http://connectsystems.be/MAINFR/AccDocument");
	root=docOut.createElement("MessageResponse");
	p=docIn.getElementsByTagName("MessageResponse").item(0);
	l=p.getChildNodes();
	n1=l.getLength();
	for(mm=0;mm<n1;++mm)
	{
		Node temp=docOut.importNode(l.item(mm),true);
		root.appendChild(temp);
	}	
	docOut.appendChild(root);
	transform.transform(new DOMSource(docOut), new StreamResult(out)); 
}
catch(Exception e)
{
	e.printStackTrace();
}


}

public void setParameter(Map arg0) {


}

public static void main(String[] args) {
try{
	RemoveSoapEnvelopSDN genFormat=new RemoveSoapEnvelopSDN();
	FileInputStream in=new FileInputStream("C:\\Apps\\my folder\\sdn\\sdn6.xml");
	FileOutputStream out=new FileOutputStream("C:\\Apps\\my folder\\sdn\\removedEnvelopSdn6.xml");
	genFormat.execute(in,out);
}
catch(Exception e)
{
e.printStackTrace();
}
}


}

You can add attributes to the tags as per your requirement. I have commnented those lines in the java code.

The output will look like this

http://postimage.org/image/2b2czww90/

Hope this helps.

Regards

Anupam

former_member854360
Active Contributor
0 Likes

Hi,

As your target system is sending the xml declaration inside the tag <?xml version="1.0"?> with the values so you need go for java or xslt mapping to remove this.

if it is possible Ask your source system not to send xml declaration . I have removed the xml declaration from the tag and found that its a well formed xml.