2011-04-01 81 views
0

我正在使用dom4j來分析xml。然後我希望能夠將這個xml表示爲Jtree。每當我以編程方式通過dom4j添加或刪除節點時,我希望更改立即反映在Jtree中。當我點擊Jtree節點時,如何捕捉事件?如何將dom4j xml轉換爲Swing Jtree?

我在http://dom4j.sourceforge.net/apidocs/

但是發現dom4j.swing包,我不知道我怎麼會去使用。我應該用哪個,我不確定。我似乎無法在這個領域找到任何示例或教程。

BranchTreeNode,DocumentTreeModel,LeafTreeNode。

import org.dom4j.Document; 
import org.dom4j.DocumentHelper; 
import org.dom4j.Element; 

public class Foo { 

    public Document createDocument() { 
     Document document = DocumentHelper.createDocument(); 
     Element root = document.addElement("root"); 

     Element author1 = root.addElement("author") 
      .addAttribute("name", "James") 
      .addAttribute("location", "UK") 
      .addText("James Strachan"); 

     Element author2 = root.addElement("author") 
      .addAttribute("name", "Bob") 
      .addAttribute("location", "US") 
      .addText("Bob McWhirter"); 

     return document; 
    } 
} 

回答