KickJava   Java API By Example, From Geeks To Geeks.

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


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.Attribute;
27 import nu.xom.Builder;
28 import nu.xom.Document;
29 import nu.xom.Element;
30 import nu.xom.Elements;
31 import nu.xom.ParsingException;
32
33
34 /**
35  *
36  * <p>
37  * Demonstrates reading the names, namespaces, and
38  * attributes of an element.
39  * </p>
40  *
41  * @author Elliotte Rusty Harold
42  * @version 1.0
43  *
44  */

45 public class TreePrinter {
46
47   public static void main(String JavaDoc[] args) {
48   
49     if (args.length == 0) {
50       System.out.println("Usage: java nu.xom.samples.TreePrinter URL");
51       return;
52     }
53       
54     Builder builder = new Builder();
55      
56     try {
57       Document doc = builder.build(args[0]);
58       Element root = doc.getRootElement();
59       listChildren(root);
60     }
61     // indicates a well-formedness error
62
catch (ParsingException ex) {
63       System.out.println(args[0] + " is not well-formed.");
64       System.out.println(ex.getMessage());
65     }
66     catch (IOException JavaDoc ex) {
67       System.out.println(ex);
68     }
69   
70   }
71   
72
73   // Print the properties of each element
74
public static void inspect(Element element) {
75     
76     if (element.getParent() != null) {
77       // Print a blank line to separate it from the previous
78
// element.
79
System.out.println();
80     }
81     
82     String JavaDoc qualifiedName = element.getQualifiedName();
83     System.out.println(qualifiedName + ":");
84     
85     String JavaDoc namespace = element.getNamespaceURI();
86     if (!namespace.equals("")) {
87       String JavaDoc localName = element.getLocalName();
88       String JavaDoc uri = element.getNamespaceURI();
89       String JavaDoc prefix = element.getNamespacePrefix();
90       System.out.println(" Local name: " + localName);
91       System.out.println(" Namespace URI: " + uri);
92       if (!"".equals(prefix)) {
93         System.out.println(" Namespace prefix: " + prefix);
94       }
95     }
96     for (int i = 0; i < element.getAttributeCount(); i++) {
97         Attribute attribute = element.getAttribute(i);
98         String JavaDoc name = attribute.getQualifiedName();
99         String JavaDoc value = attribute.getValue();
100         System.out.println(" " + name + "=\"" + value + "\"");
101     }
102     
103     for (int i = 0; i < element.getNamespaceDeclarationCount(); i++) {
104         String JavaDoc additional = element.getNamespacePrefix(i);
105         String JavaDoc uri = element.getNamespaceURI(additional);
106         System.out.println(
107           " xmlns:" + additional + "=\"" + uri + "\"");
108     }
109     
110   }
111   
112   public static void listChildren(Element current) {
113    
114     inspect(current);
115     Elements children = current.getChildElements();
116     for (int i = 0; i < children.size(); i++) {
117       listChildren(children.get(i));
118     }
119     
120   }
121
122 }
123
Popular Tags