September 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Going Beyond XML Manipulation with Apache AXIOM

  • March 19, 2009
  • By Deepal Jayasinghe
  • Send Email »
  • More Articles »

With the popularity of Apache Axis2, Axiom also got a good attention. One of the cool things is that you don’t need to have Axis2 to use Axiom, you can download Axiom separately and use specifically. Axiom is used in a number of Apache project and other project in addition to Apache Axis2. In the first part of the article I discussed Pull and Push parser technology as well as gave an Axiom overview. I also showed how to create Axiom in various ways and how to work with attributes and namespaces. So if you followed the first part, then you have good background knowledge to follow this article. If you have not followed the first part then it is recommended that you read it before continuing.

Tree Navigation

Navigating the object structure can be done in the usual way by using the list of children (similar to any of the XML processing tool). However in Axiom, the child nodes are returned as an iterator. The idea of iterator is to support “Pull parsing” and ”On-Demand” building, so only the element you asked for will be read into the memory. Others will remain untouched. The following code sample shows how the children can be accessed. The children are of the type OMNode and can either be of type OMText or OMElement.

Get all Nodes

Iterator children = root.getChildren();
       while(children.hasNext()){
       OMNode node = (OMNode)children.next();
}

Get Only Elements

You can use following code to all child OMElement for a given Element, in this case you will not get any OMText element.

Iterator children = root.getChildElements();
       while(children.hasNext()){
       OMElement node = (OMElement)children.next();
}

Get Element by Name

If you know the Qname (full qualified name) of the element then you can get that from any given element as follows.

 OMElement element = root.getFirstChildWithName(new Qname("myElement"));

Get Elements by Name

If you have a list of elements with the same name, then you can get all of them by using the following command,

Iterator children = root.getChildrenWithName()

In addition to this way, every OMNode has links to its siblings. If more thorough navigation is needed the nextSibling() and PreviousSibling() methods can be used.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel