How Can I Evaluate an XPath with AXIOM?

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 Eran Chinthaka
  • 22 Sep, 2006

You need to download an AXIOM release. We encourage you to use Axiom 1.1.1, as it is the latest, most stable version. Once you download the release, extract it to a folder. Then add all the jars found inside "build" and "lib" folders in to your classpath. Now let's get in to the real work.

Let's take following XML as an example.

<Name>Dihini Himahansi< /Name>

<Country>Sri Lanka</Country>
<Name>Thushari Damayanthi</Name>
<Country>Sri Lanka</Country>

Let's try to get access to all the person elements of this XML fragment. So the xpath will be "/Persons/Person"

Step 1

We need to create an OMElement from the source XML. Let's create an OMBuilder to build AXIOM tree, passing an InputStream from the source xml to the builder.

OMElement documentElement = new

Step 2

Create an instance of AXIOMXpath, passing your xpath expression.

AXIOMXPath xpathExpression = new AXIOMXPath ("/Persons/Person"); 

Step 3

Apply the xpath on the OMElement created in step 1.

List nodeList = (OMNode)

We've asked AXIOMXPath to return all the matching nodes. But a single node can be selected by calling selectSingleNode() method, which provides the first matching element.

You can download the complete code sample.

OMNode is the base class of all the information items implemented in AXIOM. It can be an Element, Text, Comment, Processing Instruction, etc. If you want to just see what is returned, you can always call the toString() method of OMNode to see what it is. For more information on working with OMNodes and AXIOM, please refer OM Tutorial.

Applies To:

Apache AXIOM/Java 1.1.1

More Information:

AXIOM Tutorial on official web site :

Introducing AXIOM :