I want to check my understanding of the technique to stream large XML
instance documents without building the document in memory.  It is
implied, I believe, that the data that is to be serialized as a XML
instance document will first exist in the file system, a database,
perhaps a domain specific object model, etc.

No doubt this has been discussed many times, I found this thread that
covers the basics of the topic:
http://marc.info/?l=axis-user&m=117393850127293&w=2

1) Create an OMDataSource, either by implementing the
org.apache.axiom.om.OMDataSource interface directly or using a
convenience class such as org.apache.axiom.om.ds.ByteArrayDataSource.

In my case, my "backing object" is a domain specific object model.  It
is up to me to ensure that the OMDataSource of my choosing is properly
backed by my object model.  In this case, then, I must write code to
"stringify" the state of my object model so it can be passed to the
ByteArrayDataSource constructor as an array of bytes (i.e. byte[]).

Question: Is my basic understanding of the OMDataSource interface correct?


2) In my work, I'm using Axiom and Axis2 in two different use cases:
*) Streaming XML from an Axis2 Web Service to a client
*) Serializing XML directly from a client side application to disk.

In both cases, the data is the domain specific object model as mentioned.

Interestingly, the "intent" of the use of OMDataSource seems quite
different when used server side vice client side.  (Client side use
simply means using Axiom to create an OMElement and serialize it's
state to disk).

On the server, an OMElement is created (backed by the OMDataSource)
and passed off to Axis2 infrastructure and handled accordingly.

When serializing XML directly from the client, the use of OMDataSource
(and associated OMElement) does not really seem to buy me much.  In
other words, most of the work that goes into "populating" the
OMDataSource implementing class (such as ByteArrayDataSource) or
creating an implementation of my own will be in "stringifying" my
object model.

By the time I get that code written, I can just as well do something like:

String xml = objectModel.toXmlString()

and write that XML to disk.  It might make sense if I'm already using
Axiom heavily in my client application, but in and of itself most of
the work seems to be in "stringifying" my object model.

My point: I welcome any observations or clarifications on my thoughts.
I'm fairly new to Axiom and Axis2 so there may be many subtleties or
features I'm not aware of.


3) I'm including my test code.  I create a simple object model and then:
*) "Stringify" it directly to disk
*) Create an OMElement backed by a ByteArrayDataSource, then serialize the
   OMElement to disk.
Note that I don't create an OMSourcedElementImpl.  It seems to work simply by:
OMElement aElement = factory.createOMElement(omDataSource, "A", namespace)


4) I'm using Axiom 1.2.8.


V/r


--------------------------------------------------------------
public class Driver {

    public A createObjectModel() {
        A a = new A();
        B b = new B();
        C c = new C();

        c.setCProp("c1");
        b.addC(c);
        b.setBProp("b1");
        c = new C();
        c.setCProp("c2");
        b.addC(c);
        a.addFie(b);

        b = new B();
        c = new C();
        c.setCProp("c3");
        b.addC(c);
        b.setBProp("b2");
        a.addFie(b);

        return a;
    }

    private String serializeAsString(A a) {
        StringBuilder xml = new StringBuilder();
        if (null != a) {
            xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            xml.append("\n");
            xml.append("<A xmlns=\"www.example.org\">");
            for (B b : a.getBList()) {
                xml.append("\n  ");
                xml.append("<B>");
                if (null != b.getBProp()) {
                    xml.append("\n");
                    xml.append("
<BProp>").append(b.getBProp()).append("</BProp>");
                }
                for (C c : b.getCList()) {
                    xml.append("\n    ");
                    xml.append("<C>");
                    if (null != c.getCProp()) {
                        xml.append("\n");
                        xml.append("
<CProp>").append(c.getCProp()).append("</CProp>");
                    }
                    xml.append("\n    ");
                    xml.append("</C>");
                }
                xml.append("\n  ");
                xml.append("</B>");
            }
            xml.append("\n");
            xml.append("</A>");
        }
        return xml.toString();
    }

    private void serializeAsDataSource(A a) throws XMLStreamException {
        String xml = this.serializeAsString(a);
        OMDataSource source = new ByteArrayDataSource(xml.getBytes(), "UTF-8");
        OMFactory factory = OMAbstractFactory.getOMFactory();
        OMNamespace ns = factory.createOMNamespace("www.example.org", "ex");
        OMElement aElement = factory.createOMElement(source, "A", ns);
        aElement.getBuilder().setCache(false);
        StringWriter writer = new StringWriter();
        aElement.serializeAndConsume(writer);
        System.out.println("----- Axiom OMElement Serialization -----");
        System.out.println(writer);
    }

    public static void main(String[] args) {
        Driver driver = new Driver();
        A a = driver.createObjectModel();
        System.out.println("----- Normal String Serialization -----");
        System.out.println(driver.serializeAsString(a));
        try {
            driver.serializeAsDataSource(a);
        } catch (XMLStreamException ex) {
            Logger.getLogger(Driver.class.getName()).log(Level.SEVERE,
null, ex);
        }
    }
}


--
Welcome to the black hole that is bicycling.

Reply via email to