1 16 19 21 import java.io.FileInputStream ; 22 import java.io.OutputStreamWriter ; 23 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import javax.xml.transform.OutputKeys ; 26 import javax.xml.transform.Transformer ; 27 import javax.xml.transform.TransformerFactory ; 28 import javax.xml.transform.dom.DOMSource ; 29 import javax.xml.transform.stream.StreamResult ; 30 31 import org.apache.xpath.domapi.XPathEvaluatorImpl; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.xpath.XPathEvaluator; 35 import org.w3c.dom.xpath.XPathNSResolver; 36 import org.w3c.dom.xpath.XPathResult; 37 import org.xml.sax.InputSource ; 38 39 54 public class ApplyXPathDOM 55 { 56 protected String filename = null; 57 protected String xpath = null; 58 59 60 public void doMain(String [] args) 61 throws Exception  62 { 63 filename = args[0]; 64 xpath = args[1]; 65 66 if ((filename != null) && (filename.length() > 0) 67 && (xpath != null) && (xpath.length() > 0)) 68 { 69 System.out.println("Loading classes, parsing "+filename+", and setting up serializer"); 73 74 InputSource in = new InputSource (new FileInputStream (filename)); 76 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 77 dfactory.setNamespaceAware(true); 78 Document doc = dfactory.newDocumentBuilder().parse(in); 79 80 Transformer serializer = TransformerFactory.newInstance().newTransformer(); 82 serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 83 84 System.out.println("Querying DOM using "+xpath); 86 87 XPathEvaluator evaluator = new XPathEvaluatorImpl(doc); 89 XPathNSResolver resolver = evaluator.createNSResolver(doc); 90 91 XPathResult result = (XPathResult)evaluator.evaluate(xpath, doc, resolver, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null); 93 94 95 System.out.println("<output>"); 97 98 Node n; 99 while ((n = result.iterateNext())!= null) 100 { 101 if (isTextNode(n)) { 102 StringBuffer sb = new StringBuffer (n.getNodeValue()); 106 for ( 107 Node nn = n.getNextSibling(); 108 isTextNode(nn); 109 nn = nn.getNextSibling() 110 ) { 111 sb.append(nn.getNodeValue()); 112 } 113 System.out.print(sb); 114 } 115 else { 116 serializer.transform(new DOMSource (n), new StreamResult (new OutputStreamWriter (System.out))); 117 } 118 System.out.println(); 119 } 120 System.out.println("</output>"); 121 } 122 else 123 { 124 System.out.println("Bad input args: " + filename + ", " + xpath); 125 } 126 } 127 128 129 static boolean isTextNode(Node n) { 130 if (n == null) 131 return false; 132 short nodeType = n.getNodeType(); 133 return nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.TEXT_NODE; 134 } 135 136 137 public static void main (String [] args) 138 throws Exception  139 { 140 if (args.length != 2) 141 { 142 System.out.println("java ApplyXPathDOM filename.xml xpath\n" 143 + "Reads filename.xml and applies the xpath; prints the nodelist found."); 144 return; 145 } 146 147 ApplyXPathDOM app = new ApplyXPathDOM(); 148 app.doMain(args); 149 } 150 151 } 153 | Popular Tags |