I find this utility useful for testing out XPath patterns against an xml document. The source code is HERE
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
/**
* Process an xml source, applying an XPath pattern to it, printing the affected
* nodes
*
* @author CEHJ
* @created 28 April 2004
*/
public class XPathTest {
public static void main(String[] args) {
try {
if (args.length < 2) {
System.out.println(
"Usage: java XPathTest <URL> <XPath pattern>");
System.exit(-1);
}
process(args);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Process a document against a Pattern
*/
public static void process(String[] args) throws Exception {
if (args.length < 2) {
System.out.println();
System.exit(1);
}
int numNodes = 0;
NodeList nodeList = XPathTest.process(args[0], args[1]);
if (nodeList != null && (numNodes = nodeList.getLength()) > 0) {
for (int i = 0; i < numNodes; i++) {
System.out.println(nodeList.item(i));
}
} else {
System.out.println("Nothing found.");
}
}
/**
* Process a document against a Pattern
*/
public static NodeList process(String inputSourcePath, String pattern)
throws Exception {
NodeList result = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
// Create document from URL - if it's not a url, try a file
InputStream in = null;
try {
URL url = new URL(inputSourcePath);
in = url.openStream();
} catch (MalformedURLException e) {
in = new FileInputStream(inputSourcePath);
}
Document document = builder.parse(new InputSource(in));
document.getDocumentElement().normalize();
XPath xpath = XPathFactory.newInstance().newXPath();
/*
* If namespaces being used NamespaceContextImpl nsctx = new
* NamespaceContextImpl(); nsctx.setNamespaceURI("xhtml",
* "http://www.w3.org/1999/xhtml"); xpath.setNamespaceContext(nsctx);
*/
// Evaluate the document against the pattern
NodeList nodeList = (NodeList) xpath.evaluate(pattern, document,
XPathConstants.NODESET);
if (nodeList.getLength() > 0) {
result = nodeList;
}
return result;
}
}
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
/**
* Process an xml source, applying an XPath pattern to it, printing the affected
* nodes
*
* @author CEHJ
* @created 28 April 2004
*/
public class XPathTest {
public static void main(String[] args) {
try {
if (args.length < 2) {
System.out.println(
"Usage: java XPathTest <URL> <XPath pattern>");
System.exit(-1);
}
process(args);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Process a document against a Pattern
*/
public static void process(String[] args) throws Exception {
if (args.length < 2) {
System.out.println();
System.exit(1);
}
int numNodes = 0;
NodeList nodeList = XPathTest.process(args[0], args[1]);
if (nodeList != null && (numNodes = nodeList.getLength()) > 0) {
for (int i = 0; i < numNodes; i++) {
System.out.println(nodeList.item(i));
}
} else {
System.out.println("Nothing found.");
}
}
/**
* Process a document against a Pattern
*/
public static NodeList process(String inputSourcePath, String pattern)
throws Exception {
NodeList result = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = factory.newDocumentBuilder();
// Create document from URL - if it's not a url, try a file
InputStream in = null;
try {
URL url = new URL(inputSourcePath);
in = url.openStream();
} catch (MalformedURLException e) {
in = new FileInputStream(inputSourcePath);
}
Document document = builder.parse(new InputSource(in));
document.getDocumentElement().normalize();
XPath xpath = XPathFactory.newInstance().newXPath();
/*
* If namespaces being used NamespaceContextImpl nsctx = new
* NamespaceContextImpl(); nsctx.setNamespaceURI("xhtml",
* "http://www.w3.org/1999/xhtml"); xpath.setNamespaceContext(nsctx);
*/
// Evaluate the document against the pattern
NodeList nodeList = (NodeList) xpath.evaluate(pattern, document,
XPathConstants.NODESET);
if (nodeList.getLength() > 0) {
result = nodeList;
}
return result;
}
}