1 import org.xml.sax.*;
\r
2 import org.w3c.dom.*;
\r
3 import javax.xml.transform.*;
\r
4 import javax.xml.transform.dom.*;
\r
5 import javax.xml.transform.stream.*;
\r
6 import java.util.ArrayList;
\r
7 import net.sf.saxon.Configuration;
\r
8 import net.sf.saxon.lib.NamespaceConstant;
\r
9 import net.sf.saxon.om.DocumentInfo;
\r
10 import net.sf.saxon.om.NodeInfo;
\r
11 import net.sf.saxon.xpath.XPathFactoryImpl;
\r
12 import org.xml.sax.InputSource;
\r
14 import javax.xml.namespace.QName;
\r
15 import javax.xml.transform.sax.SAXSource;
\r
16 import javax.xml.xpath.*;
\r
17 import java.io.BufferedReader;
\r
18 import java.io.File;
\r
19 import java.io.InputStreamReader;
\r
20 import java.util.Iterator;
\r
21 import java.util.List;
\r
22 import net.sf.saxon.type.Type;
\r
25 public class XPathExample {
\r
28 public static void main (String args[]) {
\r
30 if (args.length != 2) {
\r
31 System.err.println("Usage: java XPathEvalSaxon input-file '/xpath/expression'");
\r
37 System.setProperty("javax.xml.xpath.XPathFactory:"+NamespaceConstant.OBJECT_MODEL_SAXON,
\r
38 "net.sf.saxon.xpath.XPathFactoryImpl");
\r
40 XPathFactory xpf = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON);
\r
42 XPath xpe = xpf.newXPath();
\r
44 System.err.println("Loaded XPath Provider " + xpe.getClass().getName());
\r
46 long startTime = System.nanoTime ();
\r
47 InputSource is = new InputSource(args[0]);
\r
48 SAXSource ss = new SAXSource(is);
\r
49 Configuration config = ((XPathFactoryImpl) xpf).getConfiguration();
\r
50 NodeInfo doc = config.buildDocumentTree(ss).getRootNode();
\r
51 long stopTime = System.nanoTime ();
\r
52 System.err.println ("Loading document : " + (stopTime - startTime) / 1000000 + "ms");
\r
54 startTime = System.nanoTime ();
\r
55 XPathExpression cexpr = xpe.compile(args[1]);
\r
56 stopTime = System.nanoTime ();
\r
57 System.err.println ("Compiling XPath expression : " + (stopTime - startTime) / 1000000 + "ms");
\r
59 startTime = System.nanoTime ();
\r
60 @SuppressWarnings("unchecked")
\r
61 ArrayList<NodeInfo> nodes = (ArrayList<NodeInfo>) cexpr.evaluate(doc, XPathConstants.NODESET);
\r
62 stopTime = System.nanoTime ();
\r
63 long firstRun = (stopTime - startTime);
\r
64 System.err.println ("Evaluating XPath expression (1st run): " + firstRun / 1000000 + "ms");
\r
66 startTime = System.nanoTime ();
\r
67 for (int i = 1; i < 10; i++) {
\r
69 cexpr.evaluate(doc, XPathConstants.NODESET);
\r
73 stopTime = System.nanoTime ();
\r
74 System.err.println ("Evaluating XPath expression (average 10 runs): " + (stopTime - startTime + firstRun) / (10 * 1000000) + "ms");
\r
76 Transformer serializer = TransformerFactory.newInstance().newTransformer();
\r
77 serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
\r
78 StreamResult o = new StreamResult(System.out);
\r
79 startTime = System.nanoTime();
\r
80 System.out.println("<xml_result num=\"1\">");
\r
81 for(int i = 0; i < nodes.size(); i++){
\r
82 NodeInfo n = nodes.get(i);
\r
83 switch (n.getNodeKind()) {
\r
84 case Type.ATTRIBUTE:
\r
85 System.out.print (n.getLocalPart() + "=" + n.getStringValue());
\r
88 serializer.transform(n, o);
\r
91 System.out.println();
\r
93 System.out.println("</xml_result>");
\r
94 stopTime = System.nanoTime();
\r
95 System.err.println ("Serializing document : " + (stopTime - startTime) / 1000000 + "ms");
\r
97 } catch (XPathException e) {
\r
98 System.out.println (e.getCause());
\r
99 } catch (Exception e) {
\r
100 System.out.println(e);
\r