Java tutorial
//package com.java2s; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.xml.namespace.NamespaceContext; import javax.xml.parsers.DocumentBuilder; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class Main { /** W3C DOM document builder. */ private static DocumentBuilder docBuilder; public static List<Node> selectMultipleNodes(String expression, InputStream is, NamespaceContext namespaceContext) throws IOException, SAXException, XPathException { Node node = createDocument(is); return selectMultipleNodes(expression, node, namespaceContext); } public static List<Node> selectMultipleNodes(String expression, Node node, NamespaceContext namespaceContext) throws XPathException { XPath xpath = XPathFactory.newInstance().newXPath(); xpath.setNamespaceContext(namespaceContext); NodeList nodeList = (NodeList) xpath.evaluate(expression, node, XPathConstants.NODESET); List<Node> nodes = new ArrayList<Node>(); int nodeCount = nodeList.getLength(); for (int i = 0; i < nodeCount; i++) { nodes.add(nodeList.item(i)); } return nodes; } public static Document createDocument(InputStream is) throws IOException, SAXException { return docBuilder.parse(is); } }