Thursday, April 18, 2013

WPS : Converting XML String to DataObject & DataObject to XMLString

Serializing and Deserializing Business Objects From and To XML Documents in WebSphere Integration Developer


Use BOXMLSerializer and BOXMLDocument interfaces to convert a Business Object to an XML string and vice versa in WebSphere Integration Developer.

Often times you will see a need to convert a given Business Object into an XML string and vice versa. You can serialize and deserialize a Business Object to and from a given XML string by using com.ibm.websphere.bo.BOXMLSerializer and com.ibm.websphere.bo.BOXMLDocument interfaces.

BOXMLSerializer serializer = (BOXMLSerializer)new ServiceManager().
                              locateService(“com/ibm/websphere/bo/BOXMLSerializer”);

Sample Java code to convert a Business Object to an XML String:
Let’s say you have your Business Object stored in a variable ‘inputDataObject’ (of type commonj.sdo.DataObject)

ByteArrayOutputStream outputStream = newByteArrayOutputStream();
serializer.writeDataObject(inputDataObject,inputDataObject.getType().
getURI(),inputDataObject.getType().getName(),outputStream);
String myXMLString = outputStream.toString(“UTF-8”);

“myXMLString” will hold the XML string corresponding to the Data Object “inputDataObject”.

Sample Java code to convert a given XML String to a Business Object:

Let’s say you have the XML string stored in a variable named ‘inputXMLString’ (of type java.lang.String)

BOXMLDocument document = serializer.readXMLDocument(new ByteArrayInputStream
                                            (inputXMLString.getBytes(“UTF-8”)));
commonj.sdo.DataObject myDataObject =document.getDataObject();

“myDataObject” will hold the Business Object corresponding to the XML string “inputXMLString”.

Note: The schema definition (xsd) corresponding to the Business Object that you are trying to convert the XML string into, should be available during runtime.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.