Java Reference
In-Depth Information
Figur e 19−1. DOMTreeWalkerT reeModel display of a web.xml file
to compile and run the example. At the time of this writing, DOM Level 2 is rea-
sonably stable but is not yet an official standard. If the TreeWalker API changes
during the standardization process, it will probably break this example.
Example 19−5: DOMTreeWalkerTreeModel.java
package com.davidflanagan.examples.xml;
import org.w3c.dom.*; // Core DOM classes
import org.w3c.dom.traversal.*; // TreeWalker and related DOM classes
import org.apache.xerces.parsers.*; // Apache Xerces parser classes
import org.xml.sax.*;
// Xerces DOM parser uses some SAX classes
import javax.swing.*;
// Swing classes
import javax.swing.tree.*;
// TreeModel and related classes
import javax.swing.event.*;
// Tree-related event classes
import java.io.*;
// For reading the input XML file
/**
* This class implements the Swing TreeModel interface so that the DOM tree
* returned by a TreeWalker can be displayed in a JTree component.
**/
public class DOMTreeWalkerTreeModel implements TreeModel {
TreeWalker walker; // The TreeWalker we're modeling for JTree
/** Create a TreeModel for the specified TreeWalker */
public DOMTreeWalkerTreeModel(TreeWalker walker) { this.walker = walker; }
/**
* Create a TreeModel for a TreeWalker that returns all nodes
* in the specified document
**/
public DOMTreeWalkerTreeModel(Document document) {
DocumentTraversal dt = (DocumentTraversal)document;
walker = dt.createTreeWalker(document, NodeFilter.SHOW_ALL,null,false);
}
/**
Search WWH ::




Custom Search