/*The Code: XmlTree.java see Chapter175 From http://javafaq.nu/books28-11.html */ import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; import java.net.*; import javax.swing.*; import javax.swing.tree.*; import javax.swing.event.*; import com.sun.xml.tree.*; import com.sun.xml.parser.*; import org.w3c.dom.*; public class XmlTree extends JFrame { protected JTree m_tree; protected DefaultTreeModel m_model; protected JTextField m_location; public XmlTree() { super("XML Tree"); setSize(400, 300); m_location = new JTextField(); m_location.setText("samples\book-order.xml"); ActionListener lst = new ActionListener() { public void actionPerformed(ActionEvent e) { readXml(m_location.getText()); } }; m_location.addActionListener(lst); getContentPane().add(m_location, BorderLayout.NORTH); DefaultMutableTreeNode top = new DefaultMutableTreeNode( "Empty"); m_model = new DefaultTreeModel(top); m_tree = new JTree(m_model); m_tree.getSelectionModel().setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION); m_tree.setShowsRootHandles(true); m_tree.setEditable(false); JScrollPane s = new JScrollPane(); s.getViewport().add(m_tree); getContentPane().add(s, BorderLayout.CENTER); WindowListener wndCloser = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; addWindowListener(wndCloser); setVisible(true); } public void readXml(String sUrl) { setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); Thread runner = new Thread() { public void run () { try { URL source; try { File f = new File(sUrl); source = f.toURL(); } catch (Exception ex) { source = new URL(sUrl); } XmlDocument doc = XmlDocumentBuilder.createXmlDocument( source.toString()); ElementNode root = (ElementNode)doc.getDocumentElement(); root.normalize(); DefaultMutableTreeNode top = createTreeNode(root); m_model.setRoot(top); m_tree.treeDidChange(); } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(this, ex.toString(), "Warning", JOptionPane.WARNING_MESSAGE); } setCursor(Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR)); } } } protected DefaultMutableTreeNode createTreeNode(ElementNode root) { DefaultMutableTreeNode node = new DefaultMutableTreeNode( root.getNodeName()); for (int k=0; k 0) node.add(new DefaultMutableTreeNode(data)); } else if (nd instanceof ElementNode) { ElementNode en = (ElementNode)nd; node.add(createTreeNode(en)); } } return node; } public static void main(String argv[]) { new XmlTree(); } } /* Understanding the Code Class XmlTree Instance variables: JTree m_tree: used to display an XML document. DefaultTreeModel m_model: used to store the content of an XML document. JTextField m_location: used for entry of a file name or URL location of an XML document. Initially the JTree component receives a single node, "Empty". An ActionListener is added to the m_location text field which calls our readXml() method passing it the current text. The readXml() method loads an XML document, corresponding to the String passed as parameter, into our tree model. The body of this method is placed in a separate thread because it can be a very expensive procedure, and we want to make sure not to clog up the event dispatching thread (to retain GUI responsiveness). First the given string is treated as a file name. A File instance is created and converted to a URL. If this does not succeed, the string is treated as a URL address: */