FileDocCategorySizeDatePackage
SAXTreeViewer.javaAPI DocExample18960Thu Aug 16 16:28:00 BST 2001javaxml2

SAXTreeViewer

public class SAXTreeViewer extends JFrame
SAXTreeViewer uses Swing to graphically display an XML document.

Fields Summary
private String
vendorParserClass
Default parser to use
private JTree
jTree
The base tree to render
DefaultTreeModel
defaultTreeModel
Tree model to use
Constructors Summary
public SAXTreeViewer()

This initializes the needed Swing settings.


                 
      
        // Handle Swing setup
        super("SAX Tree Viewer");
        setSize(600, 450);
    
Methods Summary
public voidbuildTree(javax.swing.tree.DefaultTreeModel treeModel, javax.swing.tree.DefaultMutableTreeNode base, java.lang.String xmlURI)

This handles building the Swing UI tree.

param
treeModel Swing component to build upon.
param
base tree node to build on.
param
xmlURI URI to build XML document from.
throws
IOException - when reading the XML URI fails.
throws
SAXException - when errors in parsing occur.


        // Create instances needed for parsing
        XMLReader reader = 
            XMLReaderFactory.createXMLReader(vendorParserClass);
        ContentHandler jTreeContentHandler = 
            new JTreeContentHandler(treeModel, base);
        ErrorHandler jTreeErrorHandler = new JTreeErrorHandler();

        // Register content handler
        reader.setContentHandler(jTreeContentHandler);

        // Register error handler
        reader.setErrorHandler(jTreeErrorHandler);

        // Parse
        InputSource inputSource = 
            new InputSource(xmlURI);
        reader.parse(inputSource);
    
public voidinit(java.lang.String xmlURI)

This will construct the tree using Swing.

param
filename String path to XML document.

        DefaultMutableTreeNode base = 
            new DefaultMutableTreeNode("XML Document: " + 
                xmlURI);
        
        // Build the tree model
        defaultTreeModel = new DefaultTreeModel(base);
        jTree = new JTree(defaultTreeModel);

        // Construct the tree hierarchy
        buildTree(defaultTreeModel, base, xmlURI);

        // Display the results
        getContentPane().add(new JScrollPane(jTree), 
            BorderLayout.CENTER);
    
public static voidmain(java.lang.String[] args)

Static entry point for running the viewer.

        try {
            if (args.length != 1) {
                System.out.println(
                    "Usage: java javaxml2.SAXTreeViewer " +
                    "[XML Document URI]");
                System.exit(0);
            }
            SAXTreeViewer viewer = new SAXTreeViewer();
            viewer.init(args[0]);
            viewer.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }