Showing posts with label BOXMLSerializer. Show all posts
Showing posts with label BOXMLSerializer. Show all posts

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.

Sunday, April 14, 2013

WPS: BOXMLSerializer to validate XML document

XML document validation

XML documents and business objects can be validated using the validation service.
In addition, other services require certain minimum standards or they throw a runtime exception. One of these is BOXMLSerializer.
You can use the BOXMLSerializer to validate XML documents before they are processed by a service request. The BOXMLSerializer validates the structure of XML documents to determine if any of the following types of errors are present:
  • Invalid XML documents, such as those that are missing certain element tags.
  • Not well-formed XML documents, such as those that contain missing closing tags.
  • Documents containing parsing errors, such as errors in entity declaration.
When an error is discovered by the BOXMLSerializer, an exception will be thrown with problem details.
The validation can be performed for import and/or export of XML documents for the following services:
  • HTTP
  • JAXRPC web services
  • JAX-WS web services
  • JMS services
  • MQ services
For the HTTP, JAXRPC, and JAX-WS services, the BOXMLSerializer will generate exceptions in the following manner:
  • Imports –
    1. The SCA component invokes the service.
    2. The service invokes a destination URL.
    3. The destination URL responds with an invalid XML exception.
    4. The service fails with a runtime exception and message.
  • Exports –
    1. The service client invokes the service export.
    2. The service client sends an invalid XML
    3. The export fails for the service and generates an exception and message.
For the JMS and MQ messaging services, the exceptions are generated in the following manner:
  • Imports –
    1. The import invokes the JMS or MQ service.
    2. The service returns a response.
    3. The service returns an invalid XML exception.
    4. The import fails and generates a message.
  • Exports –
    1. The MQ or JMS client invokes an export.
    2. The client sends invalid XML.
    3. The export fails and generates an exception and message.
You can view the logs for any messages generated by an XML validation exception.