Hi,
The 2 basic forms I'd use are:
- "//b"; or
- "//a/b".
The // is a wildcard for any path to a 'b' element. Here's the java code I
used:
---start-----------------------------
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import java.io.FileReader;
import java.io.File;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.traversal.NodeIterator;
import org.w3c.dom.Node;
public class XPathDemo
{
public static String XML_FILE = "c:\\tmp\\xml.xml";
public static void main(String[] args)
{
try{
//load the file
FileReader fr = new FileReader(new File(XML_FILE));
InputSource input = new InputSource(fr);
//create the DOM parser
Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(input);
//run the xpath
System.out.println("Using //b");
NodeIterator it = XPathAPI.selectNodeIterator(xml, "//b");
Node node = null;
while((node=it.nextNode())!=null){
System.out.println("\t" + node.getFirstChild().getNodeValue());
}
System.out.println("Using //a/b");
it = XPathAPI.selectNodeIterator(xml, "//a/b");
node = null;
while((node=it.nextNode())!=null){
System.out.println("\t" + node.getFirstChild().getNodeValue());
}
}
catch(Exception x){
x.printStackTrace();
}
}
}
hope this helps
simon
----end -----------------------------
> Hi Folks,
> How to get the value of a node or element using xpath api.
>
> <a>
> <b>hi there</b>
> </a>
>
> i want to get "hi there" here just a hint would do.
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com
>
>