Apache AXIOM and XML Manipulations

Axiom acronym stand as AXis2 Object Model. It is the part of the Axis2 project and it is now consider as the component of WS components.

Axiom is the main XML representation mechanism in Axis2, so any message coming to Axis2 will be represented as an Axiom object in Axis2.  DOM and JDOM are two classic examples for such XML models. AXIOM is conceptually similar to such a XML model by its external behaviour, but deep down it is very different.

AXIOM i lightweight info-set representation of XML based on StAX, which is the standard streaming pull parser API. The object model can be manipulated flexibly as any other object model (such as JDOM), but the objects will be created only when they are absolutely required.

Hey Nifras! What you mean by pull parsing and push parsing?

I am hearing your mind voice. :-P 

XML documents can be parsed using a "pull-based" or a "push-based" (StAX) process. Now the trend on pull parsing is PULL parsing.The previously popular frameworks like SAX and DOM uses PUSH passing (SAX) process. 

So What is differ this ? 
pull parsing refers to a programming model in which a client application calls methods on an XML parsing library when it needs to interact with an XML infoset; that is, the client only gets (pulls) XML data when it explicitly asks for it.

push parsing refers to a programming model in which an XML parser sends (pushes) XML data to the client as the parser encounters elements in an XML infoset; that is, the parser sends the data whether or not the client is ready to use it at that time.

Now Let's work with AXIOM

If we need to work with AXIOM we need to download the AXIOM binary distributions or AXIOM source code distribution. The add the AXIOM into your class path. If you are using maven then add that into your maven dependency. 

Let's Creating AXIOM

We can create AXIOM in three ways. 

  1. create using pull event stream (StAX).
  2. create using push event stream (SAX).
  3. create using programmatically.


Let's have a look with create usgin pull event strream (StAX), 

The factory helps keep the code at the interface level and the implementations separately. Because Axiom is tightly bound to StAX, a StAX compliant reader should be created first with the desired input stream. 

Then, you can select one of the many builders available. In Axiom, you can find different types of builders as well, and those are mainly for user convenience. Axiom has OM builders as well as SOAP builders, so you can use the appropriate builder for your requirement. 

StAXOMBuilder will build a pure XML infoset-compliant object model whereas the SOAPModelBuilder returns SOAP-specific objects (such as the SOAPEnvelope, which are sub classes of the OMElement) through its builder methods.

Example 1 : Creating AXIOM from the input file stream. 

//create the parser
XMLStreamReader parser =
XMLInputFactory.newInstance().createXMLStreamReader(
new FileInputStream(file));
//create the builder
StAXOMBuilder builder = new StAXOMBuilder(parser);
//get the root element of the XML
OMElement documentElement = builder.getDocumentElement();

Example 2 : Creating AXIOM from the String

String xmlString = "<book>" +
"<name>Qucik-start Axis</name>" +
"<isbn>978-1-84719-286-8</isbn>" +
"</book>";
//Convert string into a stream
ByteArrayInputStream xmlStream =
new ByteArrayInputStream(xmlString.getBytes());
//Create a builder. Because we want the XML as a plain XML,
//we can just use the plain OMBuilder
StAXBuilder builder = new StAXOMBuilder(xmlStream);
//Return the root element.
builder.getDocumentElement();
Example 3 : Create AXIOM programmatically 
//Obtain a factory.
OMFactory factory = OMAbstractFactory.getOMFactory();
//Use the factory to create two namespace object.
OMNamespace axis2 = factory.createOMNamespace("axis2","ns");
//Use the factory to create three elements to represent the book
//element.
OMElement root = factory.createOMElement("book",axis2);
OMElement name = factory.createOMElement("name",axis2);
OMElement isbn = factory.createOMElement("isbn",axis2);

Example 4: Adding a Child Node and Attributes

The following are important method for adding axiom child and attributes. 
//Axiom child & attribute adding methods
public void addChild(OMNode omNode);
public void addAttribute(OMAttribute omAttribute);
// Now you can add the childs for the above program
//Obtain a factory.
OMFactory factory = OMAbstractFactory.getOMFactory();
//Use the factory to create two namespace object.
OMNamespace axis2 = factory.createOMNamespace("axis2","ns");
//Use the factory to create three elements to represent the book
//element.
OMElement root = factory.createOMElement("book",axis2);
OMElement name = factory.createOMElement("name",axis2);
OMElement isbn = factory.createOMElement("isbn",axis2);
//add childs
root.addChild(name);
root.addChild(isbn);
view raw gistfile1.txt hosted with ❤ by GitHub

Sample 5: Working with OM Namespaces

Namespace handling is the key part in XML processing. This will provide by some set of XML APIs.

public OMNamespace declareNamespace(String uri, String prefix);
public OMNamespace declareNamespace(OMNamespace namespace);
public OMNamespace findNamespace(String uri, String prefix)
throws OMException;
view raw namespaces.java hosted with ❤ by GitHub
<ns:book xmlns:ns="axis2">
<ns:name></ns:name>
<ns:isbn></ns:isbn>
</ns:book>
view raw serialized.xml hosted with ❤ by GitHub


6: Working with Attributes

Now we will see how to add attributes
OMAttribute type =
factory.createOMAttribute("type",null,"web-services");
root.addAttribute(type);

Comments

Popular posts from this blog

Missionaries & Canibal Problem in AI using Pro Log

Spring Boot - No Need to Restart the Development Server for Each Time