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