import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class XPathTest { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException{ final String entryXpath = "//entry/*/text()"; final String contentXpath = "//content/*"; DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(false); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.parse("/Users/nathanschwermann/Desktop/blog.xml"); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); javax.xml.xpath.XPathExpression expr = xpath.compile(contentXpath); Object result = expr.evaluate(doc, XPathConstants.STRING); System.out.println((String)result); expr = xpath.compile(entryXpath); result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList)result; System.out.println("----======printing nodes=====------"); for(int i = 0; i < nodes.getLength(); i++){ System.out.println(nodes.item(i).getNodeValue()); } } }