User login

How Can I Evaluate an XPath with AXIOM?

Story :

Project :

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.

<Persons>
<Person>
<Name>Dihini Himahansi</Name>


<Sex>Female</Sex>
<Address>
<City>Rajagiriya</City>
<District>Colombo</District>
<Country>Sri Lanka</Country>
</Address>
</Person>
<Person>
<Name>Thushari Damayanthi</Name>
<Sex>Female</Sex>
<Address>
<City>Ambalangoda</City>
<District>Galle</District>
<Country>Sri Lanka</Country>
</Address>
</Person>
</Persons>

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
StAXOMBuilder(inStreamToXML).getDocumentElement();

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)
xpathExpression.selectNodes(documentElement);

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 :http://ws.apache.org/commons/axiom/OMTutorial.html

Introducing AXIOM :http://www.theserverside.com/tt/articles/article.tss?l=Axiom

5
Average: 5 (1 vote)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I cannot make it work with

I cannot make it work with an xml that has a namespace that does not have a prefix.

if I use this, it works fine :

<person>

    <data>

        <name>John Doe</name>

    </data>

</person>

 

             XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream( FILE_PATH ));

            StAXOMBuilder stAXOMBuilder = new StAXOMBuilder(xmlStreamReader);

            

            AXIOMXPath xpathExpression = new AXIOMXPath ("/person/data/name");



            OMElement documentElement = stAXOMBuilder.getDocumentElement();

            OMElement NameOM = (OMElement) xpathExpression.selectSingleNode(documentElement);

            if (NameOM == null)    {

                System.out.println("Could not find element");

            } else    {

                System.out.println(NameOM.getText());

            }

If I use this :

<person xmlns="http://somenamespace/without/prefix">

    <data>

        <name>John Doe</name>

    </data>

</person>,

I can't make it work.

I tried  to add :

xpathExpression.addNamespace(stAXOMBuilder.getPrefix(), stAXOMBuilder.getNamespace()); //-->Does not work

I also tried :

SimpleNamespaceContext nsCtx = new SimpleNamespaceContext();

nsCtx.addNamespace(null, "http://somenamespace/without/prefix");

xpathExpression.setNamespaceContext(nsCtx);

//--> does note work

I could not find any answer anywhere except the fact that a lot of comments and articles point out that there is a problem with namespace without prefix.

If someone could shed some light on how to use  AXIOMXPath with namespaces

and eventually what is the best practice on using namespaces in relation to XPATH engines.

Thanks

Guillaume

 

After having read this

After having read this thread and this article,

I could make it work by adding a prefix to the namespace I declare for the AXIOMXpath.

It does not matter which prefix we use, but Jaxen needs one.

My code is now :

XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream( FILE_PATH ));

            StAXOMBuilder stAXOMBuilder = new StAXOMBuilder(xmlStreamReader);

           

            AXIOMXPath xpathExpression = new AXIOMXPath ("/a:person/a:data/a:name");

            xpathExpression.addNamespace("a", "http://somenamespace/without/prefix");



            OMElement documentElement = stAXOMBuilder.getDocumentElement();

            OMElement NameOM = (OMElement) xpathExpression.selectSingleNode(documentElement);

It needs refining to work on different case scenarios...

Guillaume

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.