1 //import javax.xml.xpath.*;
2 import javax.xml.xpath.XPathConstants;
3 import javax.xml.xpath.XPath;
4 import javax.xml.xpath.XPathException;
5 import net.sf.saxon.xpath.XPathFactoryImpl;
6 import net.sf.saxon.om.NodeInfo;
7 import net.sf.saxon.type.Type;
10 import javax.xml.transform.*;
11 import javax.xml.transform.dom.*;
12 import javax.xml.transform.stream.*;
13 import java.util.ArrayList;
15 public class XPathEvalSaxon {
18 public static void main(String args[]) {
22 XPath xpath = (new XPathFactoryImpl()).newXPath();
23 String expression = args[1];
24 InputSource inputSource = new InputSource(args[0]);
25 long startTime = System.nanoTime();
26 @SuppressWarnings("unchecked")
27 ArrayList<NodeInfo> nodes = (ArrayList<NodeInfo>) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
28 long evalTime = (System.nanoTime() - startTime) / 1000000;
29 Transformer serializer = TransformerFactory.newInstance().newTransformer();
30 serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
31 StreamResult o = new StreamResult(System.out);
32 startTime = System.nanoTime();
33 System.out.println("<xml_result num=\"1\">");
34 for(int i = 0; i < nodes.size(); i++){
35 NodeInfo n = nodes.get(i);
36 switch (n.getNodeKind()) {
38 System.out.print (n.getLocalPart() + "=" + n.getStringValue());
41 serializer.transform(n, o);
46 System.out.println("</xml_result>");
47 long printTime = (System.nanoTime() - startTime) / 1000000;
48 System.err.println("evaluation time: " + evalTime + "ms");
49 System.err.println("serialization time: " + printTime + "ms");
50 } catch (XPathException e) {
51 System.out.println (e.getCause());
52 } catch (Exception e) {
53 System.out.println(e);