在上一篇文章中,我們開發了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 ); }