在上一篇文章中,我们开发了Xtree类,这是一个可重用的组件,继续于Jtree类并能够把XML数据以图形树的形式显示。我们现在就增强这个类, 通过提供给它一个在显示默认的XML树来We will now enhance that class by PRoviding it with a default XML tree to display in the event that an XML file is not supplied at the command-line. 而且,我们还将添加一些错误处理逻辑以便程序不会因为无效的XML而崩溃。
root = new DefaultMutableTreeNode( "Welcome to XML View 1.0" ); instructions = new DefaultMutableTreeNode( "Instructions" ); openingDoc = new DefaultMutableTreeNode ( "Opening XML Documents" ); openingDocText = new DefaultMutableTreeNode ( "When invoking the XmlEditor from the command-line, you must specify the filename." ); editingDoc = new DefaultMutableTreeNode ( "Editing an XML Document" ); editingDocText = new DefaultMutableTreeNode ( "XML text in the right hand frame can be edited directly. The "refresh" button will rebuild the JTree in the left frame." ); savingDoc = new DefaultMutableTreeNode ( "Saving an XML Document" ); savingDocText = new DefaultMutableTreeNode ( "This iteration of the XmlEditor does not provide the ability to save your document. That will come with the next article." ); root.add( instructions ); instructions.add( openingDoc ); instructions.add( editingDoc ); openingDoc.add( openingDocText ); editingDoc.add( editingDocText ); return new DefaultTreeModel( root ); }