1 9 10 package org.dom4j.samples; 11 12 import org.dom4j.Document; 13 import org.dom4j.DocumentHelper; 14 import org.dom4j.XPath; 15 16 24 public class XPathValueOf extends SAXDemo { 25 26 protected XPath xpath; 27 28 public static void main(String [] args) { 29 run(new XPathValueOf(), args); 30 } 31 32 public XPathValueOf() { 33 } 34 35 public void run(String [] args) throws Exception { 36 if (args.length < 2) { 37 printUsage("{options} <XPath expression> <xml file(s)>"); 38 return; 39 } 40 41 for (int i = 0, size = args.length; i < size; i++) { 42 String arg = args[i]; 43 if (arg.startsWith("-")) { 44 readOptions(arg); 45 } else { 46 if (xpath == null) { 47 setXPath(arg); 48 } else { 49 Document document = parse(arg); 50 process(document); 51 } 52 } 53 } 54 } 55 56 public void setXPath(String xpathExpression) { 57 xpath = DocumentHelper.createXPath(xpathExpression); 58 } 59 60 protected void process(Document document) throws Exception { 61 String value = xpath.valueOf(document); 62 println(value); 63 } 64 65 protected void readOptions(String arg) { 66 } 67 } 68 69 107 | Popular Tags |