KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ElementScannerTest


1 import java.io.IOException JavaDoc;
2
3 import org.xml.sax.InputSource JavaDoc;
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 JavaDoc[] args) throws Exception JavaDoc
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 JavaDoc input = "test.xml";
33       if (args.length > 0)
34       {
35          input = args[0];
36       }
37
38       scanner.parse(new InputSource JavaDoc(input));
39    }
40
41    private static class Spy implements ElementListener
42    {
43       private String JavaDoc name;
44
45       public Spy(String JavaDoc name)
46       {
47          this.name = name;
48       }
49
50       public void elementMatched(String JavaDoc 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 JavaDoc ex1) { ex1.printStackTrace(); }
59       }
60    }
61 }
62
63
Popular Tags