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. 😛 
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. 

https://gist.github.com/nifrasismail/5a0ec0635b52d400ca0e3c4d141d5635.js

Example 3 : Create AXIOM programmatically 

https://gist.github.com/nifrasismail/1295accee62c98faf3116a996af68784.js

Example 4: Adding a Child Node and Attributes

The following are important method for adding axiom child and attributes. 

https://gist.github.com/nifrasismail/8c70ae94e5b25f2a308522ea66246038.js

Sample 5: Working with OM Namespaces

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

https://gist.github.com/nifrasismail/b06f9adf1541114bd7784eb379ddf917.jshttps://gist.github.com/nifrasismail/e49363610e2ecb1fca465e867c6228e5.js

6: Working with Attributes

Now we will see how to add attributes
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s