commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Darryl Fenwick <fenw...@streamsim.com>
Subject [JXPath] jxpath and namespaces
Date Fri, 12 Oct 2007 17:24:12 GMT

Hi All,

I'd like to use JXPath to access elements in an xml file with the 
following sample contents in a file called test.xml.

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore xmlns="http://www.streamsim.com/ns/2007/bookstore">
<book>
   <title lang="eng">Harry Potter</title>
   <price>29.99</price>
</book>
<book>
   <title lang="eng">Learning XML</title>
   <price>39.95</price>
</book>
</bookstore>

I have the following Java method using JXPath to read the xml file:

static public void testJXPath() throws MalformedURLException {
         File testXml = new File("C:/TEMP/test.xml");
         String xPath = "bookstore/book[0]/title";
         URI uri = testXml.toURI();
         JXPathContext context = JXPathContext.newContext(new 
DocumentContainer(uri.toURL(), DocumentContainer.MODEL_JDOM));
         context.registerNamespace("", 
"http://www.streamsim.com/ns/2007/bookstore");
         System.out.println(context.selectSingleNode(xPath).getClass().toString());
         System.out.println(context.selectSingleNode(xPath).toString());
         System.out.println(context.getValue(xPath).getClass().toString());
         System.out.println(context.getValue(xPath).toString());
}

This causes an exception:

No pointer for xpath: bookstore/book[0]/title
org.apache.commons.jxpath.JXPathException: No pointer for xpath: 
bookstore/book[0]/title
         at 
org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getPointer(JXPathContextReferenceImpl.java:402)
         at 
org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.getPointer(JXPathContextReferenceImpl.java:392)
         at 
org.apache.commons.jxpath.JXPathContext.selectSingleNode(JXPathContext.java:639)


However, when I remove the namespace from the xml file, the method 
runs fine with the following output.

------------- Standard Output ---------------
class org.jdom.Element
[Element: <title/>]
class java.lang.String
Harry Potter

Can anyone tell me how do I get this to work with the namespace in 
the xml?  I thought that the registerNamespace would work, but I 
guess I don't understand what it does.

Thanks in advance for the help!

Darryl




Darryl Fenwick
Senior Product Manager
Streamsim Technologies, Inc
204 Ramona St
Palo Alto, CA 94301
+1 650 557 2196 




---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message