SAXTreeViewerpublic class SAXTreeViewer extends JFrame SAXTreeViewer uses Swing to graphically
display an XML document. |
Fields Summary |
---|
private String | vendorParserClassDefault parser to use | private JTree | jTreeThe base tree to render | DefaultTreeModel | defaultTreeModelTree 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 void | buildTree(javax.swing.tree.DefaultTreeModel treeModel, javax.swing.tree.DefaultMutableTreeNode base, java.lang.String xmlURI)This handles building the Swing UI tree.
// 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 void | init(java.lang.String xmlURI) This will construct the tree using Swing.
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 void | main(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 void | setNamespaceProcessing(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);
|
|