1 21 22 package nu.xom.samples; 23 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 ; 29 30 import nu.xom.Builder; 31 import nu.xom.Document; 32 import nu.xom.Element; 33 import nu.xom.Elements; 34 35 46 public class TreeViewer { 47 48 public static MutableTreeNode processElement(Element element) { 50 51 String data; 52 if (element.getNamespaceURI().equals("")) 53 data = element.getLocalName(); 54 else { 55 data = 56 '{' 57 + element.getNamespaceURI() 58 + "} " 59 + element.getQualifiedName(); 60 } 61 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); 66 } 67 68 return node; 69 70 } 71 72 public static void display(Document doc) { 73 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"); 78 79 80 String version = System.getProperty("java.version"); 81 if (version.startsWith("1.2") || version.startsWith("1.1")) { 82 f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 83 } 84 else { 85 f.setDefaultCloseOperation(3); 88 } 89 f.getContentPane().add(treeView); 90 f.pack(); 91 f.show(); 92 93 } 94 95 public static void main(String [] args) { 96 97 try { 98 Builder builder = new Builder(); 99 for (int i = 0; i < args.length; i++) { 100 Document doc = builder.build(args[i]); 101 display(doc); 102 } 103 } 104 catch (Exception ex) { 105 System.err.println(ex); 106 } 107 108 } 110 } | Popular Tags |