1 61 62 63 import javax.xml.parsers.DocumentBuilder ; 64 import javax.xml.parsers.DocumentBuilderFactory ; 65 66 import org.w3c.dom.Document ; 67 68 import org.jaxen.dom.DOMXPath; 69 70 import org.jaxen.XPath; 71 import org.jaxen.XPathSyntaxException; 72 import org.jaxen.JaxenException; 73 74 import java.util.List ; 75 import java.util.Iterator ; 76 77 public class DOMDemo 78 { 79 public static void main(String [] args) 80 { 81 if ( args.length != 2 ) 82 { 83 System.err.println("usage: DOMDemo <document url> <xpath expr>"); 84 System.exit( 1 ); 85 } 86 87 try 88 { 89 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 90 factory.setNamespaceAware(true); 91 DocumentBuilder builder = factory.newDocumentBuilder(); 92 93 Document doc = builder.parse( args[0] ); 94 95 XPath xpath = new DOMXPath( args[1] ); 96 97 System.out.println( "XPah:h " + xpath ); 98 99 List results = xpath.selectNodes( doc ); 100 101 Iterator resultIter = results.iterator(); 102 103 System.out.println("Document :: " + args[0] ); 104 System.out.println(" XPath :: " + args[1] ); 105 System.out.println(""); 106 System.out.println("Results" ); 107 System.out.println("----------------------------------"); 108 109 while ( resultIter.hasNext() ) 110 { 111 System.out.println( resultIter.next() ); 112 } 113 } 114 catch (XPathSyntaxException e) 115 { 116 System.err.println( e.getMultilineMessage() ); 117 } 118 catch (JaxenException e) 119 { 120 e.printStackTrace(); 121 } 122 catch (Exception e) 123 { 124 e.printStackTrace(); 125 } 126 } 127 } 128 | Popular Tags |