Monday, June 4, 2012

WPS : serialisation of XML Dataobject to String and XML string to XML serialized DataObject

Introduction : We required to serialize the the DataObject to XML string and also serialize XML String to DataObject.

imports :
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import com.ibm.websphere.bo.BOXMLDocument;
import com.ibm.websphere.bo.BOXMLSerializer;
import commonj.sdo.DataObject;
import commonj.sdo.Type;
DataObejct to XMLString :


// dataObject is input DataObject
      String xmlSrting = "";
        BOXMLSerializer boXMLSerializer = (com.ibm.websphere.bo.BOXMLSerializer) com.ibm.websphere.sca.ServiceManager.INSTANCE.locateService("com/ibm/websphere/bo/BOXMLSerializer");
      
        Type type = dataObject.getType();
        BOXMLDocument document = boXMLSerializer.createXMLDocument(dataObject,type.getURI(),type.getName());
      
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            boXMLSerializer.writeXMLDocument(document,outputStream);
            return new String(outputStream.toByteArray(),document.getEncoding());
              } catch (UnsupportedEncodingException e) {
            throw e;
        } catch (IOException ex) {
            throw ex;
        }


XMLString to DataObejct :

BOXMLSerializer boXMLSerializer = (com.ibm.websphere.bo.BOXMLSerializer) com.ibm.websphere.sca.ServiceManager.INSTANCE
        .locateService("com/ibm/websphere/bo/BOXMLSerializer");
       
        byte[] bytes = businessObjectXML.getBytes();
        BOXMLDocument document = boXMLSerializer.readXMLDocument(new ByteArrayInputStream(bytes));
        return document.getDataObject();

No comments:

Post a Comment

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