1
/* Copyright 2002, 2003 Elliotte Rusty Harold
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.
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.
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
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/
22
package nu.xom.samples;
24
import javax.swing.JFrame;
25
import javax.swing.JScrollPane;
26
import javax.swing.JTree;
27
import javax.swing.tree.DefaultMutableTreeNode;
28
import javax.swing.tree.MutableTreeNode;
30
import nu.xom.Builder;
31
import nu.xom.Document;
32
import nu.xom.Element;
33
import nu.xom.Elements;
38
* Demonstrates using Swing to present a graphical display of
39
* the tree structure of an XML document.
42
* @author Elliotte Rusty Harold
46
public class TreeViewer {
48
// Initialize the per-element data structures
49
public static MutableTreeNode processElement(Element element) {
52
if (element.getNamespaceURI().equals(""))
53
data = element.getLocalName();
57
+ element.getNamespaceURI()
59
+ element.getQualifiedName();
62
MutableTreeNode node = new DefaultMutableTreeNode(data);
63
Elements children = element.getChildElements();
64
for (int i = 0; i < children.size(); i++) {
65
node.insert(processElement(children.get(i)), i);
72
public static void display(Document doc) {
74
Element root = doc.getRootElement();
75
JTree tree = new JTree(processElement(root));
76
JScrollPane treeView = new JScrollPane(tree);
77
JFrame f = new JFrame("XML Tree");
80
String version = System.getProperty("java.version");
81
if (version.startsWith("1.2") || version.startsWith("1.1")) {
82
f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
85
// JFrame.EXIT_ON_CLOSE == 3 but this named constant is not
86
// available in Java 1.2
87
f.setDefaultCloseOperation(3);
89
f.getContentPane().add(treeView);
95
public static void main(String[] args) {
98
Builder builder = new Builder();
99
for (int i = 0; i < args.length; i++) {
100
Document doc = builder.build(args[i]);
104
catch (Exception ex) {
105
System.err.println(ex);
b'\\ No newline at end of file'