Java by API/javax.xml.xpath/XPath
XPath: compile(String expression) throws XPathExpressionException
<source lang="java">
import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Hashtable; import javax.xml.namespace.QName; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; import javax.xml.xpath.XPathVariableResolver; import org.w3c.dom.Document; import org.w3c.dom.Element; public class Main {
public static void main(String[] args) throws Exception { SimpleDateFormat xmlDateFormat = new SimpleDateFormat("MM.dd.yy"); MapVariableResolver resolver = new MapVariableResolver(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbf.newDocumentBuilder(); Document document = builder.parse(new File("t.xml")); XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); xPath.setXPathVariableResolver(resolver); XPathExpression expression = xPath.rupile("/schedule/show[@date=$date]/guest"); String formattedDate = xmlDateFormat.format(new Date(2006, 5, 14)); resolver.addVariable(null, "date", formattedDate); Element guest = (Element) expression.evaluate(document, XPathConstants.NODE); System.out.println(guest.getElementsByTagName("name").item(0).getTextContent()); }
} class MapVariableResolver implements XPathVariableResolver {
private Hashtable variables = new Hashtable(); public void addVariable(String namespaceURI, String localName, Object value) { addVariable(new QName(namespaceURI, localName), value); } public void addVariable(QName name, Object value) { variables.put(name, value); } public Object resolveVariable(QName name) { Object retval = variables.get(name); return retval; }
}
</source>
XPath: evaluate(String expression, Object item)
<source lang="java">
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.Node; import org.w3c.dom.NodeList; public class MainClass {
public static void main(String[] args) throws ParserConfigurationException, XPathExpressionException, org.xml.sax.SAXException, java.io.IOException { String documentName = args[0]; String expression = args[1]; DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = parser.parse(new java.io.File(documentName)); XPath xpath = XPathFactory.newInstance().newXPath(); System.out.println(xpath.evaluate(expression, doc)); NodeList nodes = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET); for (int i = 0, n = nodes.getLength(); i < n; i++) { Node node = nodes.item(i); System.out.println(node); } }
}
</source>