import javax.xml.parsers.*; import org.w3c.dom.*; import java.io.*; import javax.xml.xpath.*; import javax.xml.namespace.QName; public class XPathTester { public static void main(String[] args) throws Exception { //Create a new factory to use to generate a DocumentBuilder DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //Make the parser aware of the namespaces factory.setNamespaceAware(true); //Create a new DocumentBuilder DocumentBuilder d = factory.newDocumentBuilder(); //Fetch the most recent Coverville feed Document doc = d.parse("http://feeds.feedburner.com/coverville"); //Create a new XPath object XPath xp = XPathFactory.newInstance().newXPath(); //Evaluate the XPath expression /rss/channel/item/title[starts-with(., \"Coverville\")] //and save the resulting NodeList NodeList nl = (NodeList) xp.evaluate("/rss/channel/item/title[starts-with(., \"Coverville\")]", doc.getDocumentElement(), XPathConstants.NODESET); //Print the contents of each title node selected for(int i = 0; i < nl.getLength(); i++) { Node n = nl.item(i); NodeList children = n.getChildNodes(); for(int j = 0; j < children.getLength(); j++) { System.out.println(children.item(j).getNodeValue()); } System.out.println("*******************\n\n"); } } }