How can I convert an LLOM AXIOM tree into a DOOM AXIOM tree?

Archived Content
This article is provided for historical perspective only, and may not reflect current conditions. Please refer to relevant product page for more up-to-date product information and resources.
  • By Ruchith Fernando
  • 30 Jul, 2007

You can convert from one Apache AXIOM implementation to another because of some key features of its design.

  • Builder and factory approach
  • If you want to create an AXIOM tree from a given StAX event stream, a 'builder' can be used with a factory to create the required elements. The builder will simply ask the factory for new XML node instances to be attached to the tree that it builds.

  • Elements can produce StAX events that describe them
  • Apache AXIOM nodes are capable of re-creating the StAX event that describes them, and they can also be used to re-create them.

Apache AXIOM provides two builders to build AXIOM structures.

  • org.apache.axiom.om.impl.builder.StAXOMBuilder
  • org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder

These two use the implementations of the following two factory interfaces to obtain the required XML/SOAP nodes.

  • org.apache.axiom.om.OMFactory
  • org.apache.axiom.soap.SOAPFactory

Now let's remind ourselves how we usually create Apache AXIOM trees.

Refer to the Eclipse project containing the source and dependencies. The following example shows one way to create an OMElement.

package org.wso2.kb.axiomdoom;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

import java.io.ByteArrayInputStream;

public class Sample1 {

public static void main(String[] args) throws Exception {

String xml = "<OxygenTank>" +
"<articles>123</articles>" +
"<tutorials>456</tutorials>" +
"</OxygenTank>";

ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());

//Create an XMLStreamReader to get the StAX events
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(bais);

//Create a new builder with the StAX reader
StAXOMBuilder builder = new StAXOMBuilder(reader);

//Get the document element
OMElement elem = builder.getDocumentElement();

//Print the element into the console
System.out.println(elem);

}
}

Now let's see how to convert this LLOM based element we created to a DOOM based one.

package org.wso2.kb.axiomdoom;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.om.impl.dom.DOOMAbstractFactory;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

import java.io.ByteArrayInputStream;

public class Sample2 {

public static void main(String[] args) throws Exception {

String xml = "<OxygenTank>" +
"<articles>123</articles>" +
"<tutorials>456</tutorials>" +
"</OxygenTank>";

ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
//Create an XMLStreamReader to get the StAX events
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(bais);
//Create a new builder with the StAX reader
StAXOMBuilder builder = new StAXOMBuilder(reader);
//Get the document element
OMElement elem = builder.getDocumentElement();

//Get the StAX reader from the created element
XMLStreamReader llomReader = elem.getXMLStreamReader();

//Create the DOOM OMFactory
OMFactory doomFactory = DOOMAbstractFactory.getOMFactory();

//Create the new builder
StAXOMBuilder doomBuilder = new StAXOMBuilder(doomFactory, llomReader);

//Get the document element
OMElement newElem = doomBuilder.getDocumentElement();

if(newElem instanceof org.w3c.dom.Element) {
System.out.println("We have a org.w3c.dom.Element !!!");
System.out.println(newElem);
}

}
}

When you run this simple program you will see the following output :

We have a org.w3c.dom.Element !!!
123456
Now let's understand what the following lines of code do:
        //Get the StAX reader from the created element
XMLStreamReader llomReader = elem.getXMLStreamReader();

//Create the DOOM OMFactory
OMFactory doomFactory = DOOMAbstractFactory.getOMFactory();

//Create the new builder
StAXOMBuilder doomBuilder = new StAXOMBuilder(doomFactory, llomReader);

//Get the document element
OMElement newElem = doomBuilder.getDocumentElement();

 

First it obtains the javax.xml.stream.XMLStreamReader instance that carries events of the "elem" instance. This is fed into a new org.apache.axiom.om.impl.builder.StAXOMBuilder instance along with the relevant DOOM factory. Then the DOOM based document element can be simply obtained from the builder.

Applies To

1. Apache AXIOM