1 package org.xmldb.common.xml.queries.test; 2 54 55 import org.w3c.dom.Document ; 56 import org.w3c.dom.NodeList ; 57 import org.xmldb.common.xml.queries.*; 58 59 import javax.xml.parsers.DocumentBuilder ; 60 import javax.xml.parsers.DocumentBuilderFactory ; 61 62 63 67 public class XPathTest { 68 69 public static void main(String [] args) throws Exception { 70 71 74 XPathQueryFactory factory = XPathQueryFactory.newInstance(); 75 76 DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); 77 DocumentBuilder docBuilder = builderFactory.newDocumentBuilder(); 78 Document doc = docBuilder.parse(args[0]); 79 80 long start = System.currentTimeMillis(); 81 XPathQuery query = factory.newXPathQuery(); 82 System.out.println(query.getClass().getName()); 83 query.setQString("/*"); 84 System.out.println("init: " + (System.currentTimeMillis() - start)); 85 86 start = System.currentTimeMillis(); 87 XObject result = query.execute(doc); 88 System.out.println("execute: " + (System.currentTimeMillis() - start)); 89 90 printResult(result); 91 92 result = query.execute(doc); 93 printResult(result); 94 } 95 96 97 protected static void printResult(XObject result) throws Exception { 98 if (result == null) { 99 System.out.println("XPath query: result: null"); 100 } else { 101 System.out.print("XPATH query: result: "); 102 switch (result.getType()) { 104 case XObject.CLASS_BOOLEAN: 105 System.out.println("(Boolean): " + result.bool()); 106 break; 107 case XObject.CLASS_NUMBER: 108 System.out.println("(Number): " + result.num()); 109 break; 110 case XObject.CLASS_STRING: 111 System.out.println("(String): " + result.str()); 112 break; 113 case XObject.CLASS_RTREEFRAG: 114 System.out.println("(DocumentFragment): -"); 115 break; 116 case XObject.CLASS_NODESET: 117 NodeList nodeList = result.nodeset(); 118 System.out.println("(NodeList): " + nodeList.getLength() + " Entries"); 119 120 for (int i = 0; i < nodeList.getLength(); i++) { 121 System.out.print(i + 1 + " Entry: "); 122 System.out.println(" value=" + nodeList.item(i).getNodeName()); 123 } 124 break; 125 default: 126 System.out.println("(Unknown): -"); 127 break; 128 } 129 } 130 } 131 132 } 133 | Popular Tags |