1 import java.io.IOException ; 2 3 import org.xml.sax.InputSource ; 4 5 import org.jdom.Element; 6 import org.jdom.output.XMLOutputter; 7 8 import org.jdom.contrib.input.scanner.ElementScanner; 9 import org.jdom.contrib.input.scanner.ElementListener; 10 11 12 public class ElementScannerTest 13 { 14 public static XMLOutputter out = new XMLOutputter(); 15 16 public static void main(String [] args) throws Exception  17 { 18 org.jdom.contrib.input.scanner.XPathMatcher.setDebug(true); 19 20 ElementScanner scanner = new ElementScanner(); 21 22 scanner.addElementListener(new Spy("Listener #1 - \"x//y\""), "x//y"); 23 scanner.addElementListener(new Spy("Listener #2 - \"y/*/y\""), "y/*/y"); 24 scanner.addElementListener(new Spy("Listener #3 - \"/*\""), "/*"); 25 scanner.addElementListener(new Spy("Listener #4 - \"z\""), "z"); 26 27 scanner.addElementListener(new Spy("Listener #5 - \"*[contains(@name,'.1')]\""), 28 "*[contains(@name,'.1')]"); 29 scanner.addElementListener(new Spy("Listener #6 - \"y[.//y]\""), 30 "y[.//y]"); 31 32 String input = "test.xml"; 33 if (args.length > 0) 34 { 35 input = args[0]; 36 } 37 38 scanner.parse(new InputSource (input)); 39 } 40 41 private static class Spy implements ElementListener 42 { 43 private String name; 44 45 public Spy(String name) 46 { 47 this.name = name; 48 } 49 50 public void elementMatched(String path, Element e) 51 { 52 try 53 { 54 System.out.print(this.name + "\n>>> " + path + "\n"); 55 out.output(e, System.out); 56 System.out.println("\n<<<\n"); 57 } 58 catch (IOException ex1) { ex1.printStackTrace(); } 59 } 60 } 61 } 62 63 | Popular Tags |