How Can I Evaluate an XPath with AXIOM?
- By Eran Chinthaka
- 22 Sep, 2006
- Reads: 15816
Apache AXIOM has in-built support for evaluating XPath expressions. Let's see how we can accomplish this.
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>
Let's try to get access to all the person elements of this XML fragment. So the xpath will be "/Persons/Person"
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
Create an instance of AXIOMXpath, passing your xpath expression.
AXIOMXPath xpathExpression = new AXIOMXPath ("/Persons/Person");
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.
Apache AXIOM/Java 1.1.1
AXIOM Tutorial on official web site :http://ws.apache.org/commons/axiom/OMTutorial.html
Introducing AXIOM :http://www.theserverside.com/tt/articles/article.tss?l=Axiom