I'm having a problem with XPATH and namespaces.
Could anyone please help ?

I'm using apache XPathAPI, with java 1.4.
It works fine as long as there are no namespaces.
Unfortunately, when adding namespaces to the xml, XPathAPI fails to find the desired element (returning null).
 
--- code ----------------------------------------
String xml=
     "< ns1:body xmlns:ns1='tempuri' >" +
          "< ns1:status > OK < / ns1:status >" +
     "< / ns1:body >";
// Parse the above xml string:
DocumentBuilder docBuilder= DocumentBuilderFactory.newInstanc().newDocumentBuilder();
Document doc=docBuilder.parse(new InputSource(new StringReader(xml)));
N ode root=doc.getDocumentElement();

// Tell XPathAPI that "ns1" is the prefix for namespace "tempuri" :
Element nsResolverNode=doc.createElement("NamespaceResolver");
nsResolverNode.setAttribute("xmlns:ns1", "tempuri");

// This returns "null", instead of the desired < status > element... why ?
Node subNode = XPathAPI.selectSingleNode(root, "ns1:body/ns1:status", nsResolverNode);
 
--- end code -------------------------------------


What am i doing wrong ? Do you know of a better way to do it ?
Thanks very much.


Brings words and photos together (easily) with
PhotoMail - it's free and works with Yahoo! Mail.

Reply via email to