KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > samples > ExampleLister


1 /* Copyright 2002, 2003 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.samples;
23
24 import java.io.IOException JavaDoc;
25
26 import nu.xom.Builder;
27 import nu.xom.Document;
28 import nu.xom.Element;
29 import nu.xom.Elements;
30 import nu.xom.ParsingException;
31
32
33 /**
34  * <p>
35  * Demonstrates extracting the content
36  * of particular named elements
37  * in a particular context
38  * from an XML document.
39  * </p>
40  *
41  * @author Elliotte Rusty Harold
42  * @version 1.0
43  *
44  */

45 public class ExampleLister {
46
47     private static int chapter = 0;
48
49     public static void list(Element root) {
50
51         chapter = 0;
52         if (root.getLocalName().equals("chapter")) {
53             chapter++;
54             exampleNumber = 0;
55             list(root);
56         }
57         else {
58             Elements elements = root.getChildElements();
59             for (int i = 0; i < elements.size(); i++) {
60                 Element child = elements.get(i);
61                 if (child.getLocalName().equals("chapter")) {
62                     chapter++;
63                     exampleNumber = 0;
64                     findExamples(child);
65                 }
66                 else {
67                     list(child);
68                 }
69             }
70             
71         }
72         
73     }
74
75     
76     private static int exampleNumber = 0;
77   
78     private static void findExamples(Element element) {
79
80         Elements elements = element.getChildElements();
81         for (int i = 0; i < elements.size(); i++) {
82             Element child = elements.get(i);
83              if (child.getQualifiedName().equals("example")) {
84                 printExample(child);
85             }
86             else {
87                 findExamples(child);
88             }
89         }
90
91     }
92
93     
94     private static void printExample(Element example) {
95
96         exampleNumber++;
97         Element title = example.getFirstChildElement("title");
98         
99         String JavaDoc caption = "Example " + chapter + "." + exampleNumber
100          + ": " + title.getValue();
101         
102        System.out.println(caption);
103     
104     }
105   
106     
107     public static void main(String JavaDoc[] args) {
108
109         if (args.length <= 0) {
110           System.out.println("Usage: java nu.xom.samples.ExampleLister URL");
111           return;
112         }
113         String JavaDoc url = args[0];
114         
115         try {
116             Builder builder = new Builder();
117             // Read the document
118
Document document = builder.build(args[0]);
119          
120              // List the examples
121
list(document.getRootElement());
122     
123         }
124         catch (ParsingException ex) {
125             System.out.println(ex);
126         }
127         catch (IOException JavaDoc ex) {
128               System.out.println(
129               "Due to an IOException, the parser could not read " + url
130             );
131             System.out.println(ex);
132         }
133      
134     } // end main
135

136 }
Popular Tags