FileDocCategorySizeDatePackage
SAXTreeViewer.javaAPI DocExample24233Thu Aug 16 16:42:30 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);        
        XMLWriter writer =
            new XMLWriter(reader, new FileWriter("snapshot.xml"));            
        NamespaceFilter filter = 
            new NamespaceFilter(writer, 
                "http://www.oreilly.com/javaxml2",
                "http://www.oreilly.com/catalog/javaxml2");
        ContentHandler jTreeContentHandler = 
            new JTreeContentHandler(treeModel, base, reader);
        ErrorHandler jTreeErrorHandler = new JTreeErrorHandler();

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

        // Register error handler
        filter.setErrorHandler(jTreeErrorHandler);
            
        // Register entity resolver
        filter.setEntityResolver(new SimpleEntityResolver());
        
        // Register lexical handler
        filter.setProperty(
            "http://xml.org/sax/properties/lexical-handler",
            jTreeContentHandler);
            
        // Register declaration handler
        filter.setProperty(
            "http://xml.org/sax/properties/declaration-handler",
            new MyDeclHandler());

        // Parse
        InputSource inputSource = 
            new InputSource(xmlURI);
        filter.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();
        }
    
private voidsetNamespaceProcessing(org.xml.sax.XMLReader reader, boolean state)

            
        reader.setFeature(
            "http://xml.org/sax/features/namespaces", state);
        reader.setFeature(
            "http://xml.org/sax/features/namespace-prefixes", !state);