當柳上原的風吹向天際的時候...

          真正的快樂來源于創造

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
          XML文件如下:
          <?xml version="1.0" encoding="GBK"?>

          <menuItems>
              
          <name>根節點</name>
              
          <icon></icon>
              
          <panelName></panelName>
              
          <mothodName></mothodName>
              
          <args></args>

              
          <menuItem>
                  
          <name>節點一</name>
                  
          <icon>folder_fromFile.gif</icon>
                  
          <panelName>
                      com.heyang.view.panel.content.folder.addfromfile.AddCategoryFromFilePanel
                  
          </panelName>
                  
          <mothodName></mothodName>
                  
          <args></args>
              
          </menuItem>
              
          <menuItem>
                  
          <name>節點二</name>
                  
          <icon>folder_attribute.gif</icon>
                  
          <panelName>
                      com.heyang.view.panel.content.folder.attribute.AttributeMngPanel
                  
          </panelName>
                  
          <mothodName></mothodName>
                  
          <args></args>
              
          </menuItem>
              
          <menuItem>
                  
          <name>節點三</name>
                  
          <icon>folder_transfer.gif</icon>
                  
          <panelName>
                      com.heyang.view.panel.content.folder.transfer.FolderTransferPanel
                  
          </panelName>
                  
          <mothodName></mothodName>
                  
          <args></args>
              
          </menuItem>
          </menuItems>

          讀取例程:
           /**
               * 按照XML文件建立一棵樹
               * 
               * 
          @author:何楊
               * @date:2009-12-22
               * @time:上午08:13:09
               
          */
              @SuppressWarnings(
          "unchecked")
              
          private void buildTree(){
                  
          // 建立樹節點
                  DefaultMutableTreeNode root = null;
                          
                  
          try {
                      SAXReader reader 
          = new SAXReader();
                      String xmlFile 
          = TreeMenuPanel.class.getResource("/text.xml").getPath()
                              .toString();
                      
                      File file
          =new File(xmlFile);
                      
          if(file.exists()==false){
                          DlgUtil.popupErrorDialog(
          "無法找到文件"+xmlFile+".");
                          
          return;
                      }
                      
                      Document document 
          = reader.read(file);
                      Element rootElm 
          = document.getRootElement();
                      
          // 遍歷XML生成節點樹
                      root=getNode(rootElm);
                  } 
          catch (Exception ex) {
                      ex.printStackTrace();
                  }
                  
                  
          // 將節點樹賦予樹組件
                  DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
                  model.setRoot(root);
                  model.reload();    
                  tree.updateUI();
              }
              
              
          /**
               * 遞歸取得節點
               * 
               * 
          @author:何楊
               * @date:2010-1-11
               * @time:上午08:31:12
               * 
          @param elm
               * 
          @return
               
          */
              @SuppressWarnings(
          "unchecked")
              
          private DefaultMutableTreeNode getNode(Element elm){        
                  String name
          =elm.elementText("name");
                  String icon
          =elm.elementText("icon");
                  String panelName
          =elm.elementText("panelName");
                  String mothodName
          =elm.elementText("mothodName");
                  String args
          =elm.elementText("args");
                  
                  MenuItem menuItem
          =new MenuItem(name,icon,panelName,mothodName,args);
                  DefaultMutableTreeNode leaf 
          = new DefaultMutableTreeNode();
                  leaf.setUserObject(menuItem);
                  
                  List
          <Element> elms = elm.elements("menuItem");
                  
          for (Element element : elms) {
                      leaf.add(getNode(element));
                  }    
                  
                  
          return leaf;
              }


          posted on 2010-01-14 21:56 何楊 閱讀(690) 評論(0)  編輯  收藏
          主站蜘蛛池模板: 九江县| 嘉黎县| 蚌埠市| 蒙山县| 云安县| 仁化县| 呼和浩特市| 河曲县| 都匀市| 盐池县| 锡林浩特市| 农安县| 山西省| 牙克石市| 西乌| 北辰区| 苗栗市| 贵港市| 大冶市| 鄂尔多斯市| 白山市| 凤翔县| 荆门市| 壤塘县| 南召县| 会昌县| 历史| 彭州市| 依兰县| 锡林浩特市| 庆城县| 灌阳县| 屏东市| 甘孜| 苍梧县| 锡林浩特市| 茶陵县| 军事| 淅川县| 盐山县| 富川|