1 import javax.xml.xpath.*;
4 import javax.xml.transform.*;
5 import javax.xml.transform.dom.*;
6 import javax.xml.transform.stream.*;
8 public class XPathEval {
11 public static void main(String args[]) {
15 XPath xpath = XPathFactory.newInstance().newXPath();
16 String expression = args[1];
17 InputSource inputSource = new InputSource(args[0]);
18 long startTime = System.nanoTime();
19 NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
20 long evalTime = (System.nanoTime() - startTime) / 1000000;
21 Transformer serializer = TransformerFactory.newInstance().newTransformer();
22 serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
23 StreamResult o = new StreamResult(System.out);
24 startTime = System.nanoTime();
25 System.out.println("<xml_result num=\"1\">");
26 for(int i = 0; i < nodes.getLength(); i++){
27 Node n = nodes.item(i);
28 switch (n.getNodeType()) {
29 case Node.ATTRIBUTE_NODE:
30 System.out.print (n.getNodeName() + "=" + n.getNodeValue());
33 serializer.transform(new DOMSource(nodes.item(i)), o);
38 System.out.println("</xml_result>");
39 long printTime = (System.nanoTime() - startTime) / 1000000;
40 System.err.println("evaluation time: " + evalTime + "ms");
41 System.err.println("serialization time: " + printTime + "ms");
42 } catch (XPathException e) {
43 System.out.println (e.getCause());
44 } catch (Exception e) {
45 System.out.println(e);