You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Stephan Rossouw <st...@easypay.co.za> on 2005/11/24 12:27:10 UTC

Im trying to traverse a XML document with JXPATH

Hi there,

 

I'm trying to traverse through an xml file with the following code and
I'm getting an error.

 

import org.apache.commons.jxpath.Container;

import org.apache.commons.jxpath.JXPathContext;

import org.apache.commons.jxpath.XMLDocumentContainer;

import org.apache.commons.jxpath.xml.DocumentContainer;

 

 class JxpathTest {

    private DocumentContainer locations = null;

   

    public Container getLocations(){

            return new DocumentContainer(

 
JxpathTest.class.getResource("wootrequest.xml"));

    }

    

    

    public static void main(String[]args) throws Exception{

            JXPathContext context = JXPathContext.newContext(new
JxpathTest());

 

             String street = (String)context.getValue(

               "locations/vendor/location[@id = 'store102']//street");

 

    }

    

}

 

 

xml:

 

<?xml version="1.0" ?>

<vendor>

       <location id="store101">

           <address>

               <street>Orchard Road</street>

           </address>

       </location>

       

       <location id="store102">

           <address>

               <street>Tangerine Drive</street>

           </address>

       </location>

</vendor>

 

error:

 

Exception in thread "main" org.apache.commons.jxpath.JXPathException: No
value for xpath: locations/vendor/location[@id = 'store102']//street

      at
org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getValue(JXPathC
ontextReferenceImpl.java:318)

      at
org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getValue(JXPathC
ontextReferenceImpl.java:280)

      at
co.za.easypay.billpayment.framework.JxpathTest.main(JxpathTest.java:22)

 

What am I doing wrong?