1 61 62 63 import org.dom4j.Document; 64 import org.dom4j.Node; 65 import org.dom4j.io.SAXReader; 66 67 import org.jaxen.XPath; 68 import org.jaxen.XPathSyntaxException; 69 import org.jaxen.JaxenException; 70 71 import org.jaxen.dom4j.Dom4jXPath; 72 73 import java.util.List ; 74 import java.util.Iterator ; 75 76 public class Dom4jDemo 77 { 78 public static void main(String [] args) 79 { 80 if ( args.length != 2 ) 81 { 82 System.err.println("usage: Dom4jDemo <document url> <xpath expr>"); 83 System.exit( 1 ); 84 } 85 86 try 87 { 88 SAXReader reader = new SAXReader(); 89 90 Document doc = reader.read( args[0] ); 91 92 XPath xpath = new Dom4jXPath( args[1] ); 93 94 List results = xpath.selectNodes( doc ); 95 96 Iterator resultIter = results.iterator(); 97 98 System.out.println("Document :: " + args[0] ); 99 System.out.println(" XPath :: " + args[1] ); 100 System.out.println(""); 101 System.out.println("Results" ); 102 System.out.println("----------------------------------"); 103 104 while ( resultIter.hasNext() ) 105 { 106 Object object = resultIter.next(); 107 if ( object instanceof Node ) 108 { 109 Node node = (Node) object; 110 System.out.println( node.asXML() ); 111 } 112 else 113 { 114 System.out.println( object ); 115 } 116 } 117 } 118 catch (XPathSyntaxException e) 119 { 120 System.err.println( e.getMultilineMessage() ); 121 } 122 catch (JaxenException e) 123 { 124 e.printStackTrace(); 125 } 126 catch (Exception e) 127 { 128 e.printStackTrace(); 129 } 130 } 131 } 132 | Popular Tags |