1 9 10 package org.dom4j.samples; 11 12 import java.util.Iterator ; 13 import java.util.List ; 14 15 import org.dom4j.Document; 16 17 23 public class XPathDemo extends SAXDemo { 24 25 protected String xpath = "*"; 26 27 public static void main(String [] args) { 28 run(new XPathDemo(), args); 29 } 30 31 public XPathDemo() { 32 } 33 34 public void run(String [] args) throws Exception { 35 if (args.length < 2) { 36 printUsage("<XML document URL> <XPath expression>"); 37 return; 38 } 39 40 String xmlFile = args[0]; 41 xpath = args[1]; 42 43 writer = createXMLWriter(); 44 45 Document document = parse(xmlFile); 46 process(document); 47 } 48 49 protected void process(Document document) throws Exception { 50 println("Evaluating XPath: " + xpath); 51 52 List list = document.selectNodes(xpath); 53 54 println("Found: " + list.size() + " node(s)"); 55 println("Results:"); 56 57 for (Iterator iter = list.iterator(); iter.hasNext();) { 58 Object object = iter.next(); 59 writer.write(object); 60 writer.println(); 61 } 62 63 writer.flush(); 64 } 65 } 66 67 105 | Popular Tags |