亲爱的耀哥哥 2016-09-01 09:03 采纳率: 100%
浏览 3859
已结题

java读取xml,将读到的数据存放到对象中,再按照一定条件写入xml,从而生成动态菜单

做eclipse插件开发,菜单项的配置是在plugin.xml中是写死的,现在想做成动态菜单的形式。中间写一个类,这个类可以读取一个xml配置文件,然后可以有一些方法把提取过的数据和plugin.xml联系起来,通过这个类来动态的配置eclipse的菜单。我的想法是把读到的xml文件的内容封装到java的对象中,返回值是一个集合类型,然后我就不知道怎么做了。

图片是展示效果:

图片说明


下面是我们找的一些相关资源:
  [](http://dom4j.sourceforge.net/dom4j-1.6.1/guide.html "")

这是我自己写的demo:

package testforxybc;
public class UserInfo {

    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    private String password;
}


package testforxybc;

import java.beans.XMLDecoder;
import java.io.File;
import java.io.FileInputStream; 
import java.io.FileNotFoundException;  
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List;

public class UserInfoController {

    public static void main(String args[])  
    {   
       List<UserInfo> objList = null;
       try {
        objList = UserInfoController.objectXMLDecoder("outxml.xml");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

       for (UserInfo userInfo : objList) {
           System.out.println(userInfo.getName());
           System.out.println(userInfo.getPassword());
       }
    }

    public static List<UserInfo> objectXMLDecoder(String objSource)    
     throws FileNotFoundException,IOException,Exception   
    {   
     List<UserInfo> objList = new ArrayList<UserInfo>();       
     File fin = new File(objSource);   
     FileInputStream fis = new FileInputStream(fin);   
     XMLDecoder decoder = new XMLDecoder(fis);   
     Object obj = null;   
     try   
     {   
      while( (obj = decoder.readObject()) != null)   
      {   
       objList.add((UserInfo) obj);
      }   
     }   
     catch (Exception e)   
     {   
      // TODO Auto-generated catch block       
     }   
     fis.close();   
     decoder.close();        
     return objList;   
    }   
}


<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

   <extension
         point="org.eclipse.ui.commands">
      <category
            name="Inoherb Category"
            id="com.uds.inoherb.commands.category">
      </category>
      <command
            name="法规标准库"
            categoryId="com.uds.inoherb.commands.category"
            id="com.uds.inoherb.commands.regulatoryStandards">
      </command>

      <command
            name="产品标准"
            categoryId="com.uds.inoherb.commands.category"
            id="com.uds.inoherb.commands.productStandard">
      </command>
      <command
            name="禁限用清单"
            categoryId="com.uds.inoherb.commands.category"
            id="com.uds.inoherb.commands.prohibitedList">
      </command>
      <command
            name="全成分"
            categoryId="com.uds.inoherb.commands.category"
            id="com.uds.inoherb.commands.fullComposition">
      </command>

      <command
            name="原料库"
            categoryId="com.uds.inoherb.commands.category"
            id="com.uds.inoherb.commands.rawMaterial">
      </command>
      <command
            name="油脂类"
            categoryId="com.uds.inoherb.commands.category"
            id="com.uds.inoherb.commands.fadAndOil">
      </command>
      <command
            name="乳化剂类"
            categoryId="com.uds.inoherb.commands.category"
            id="com.uds.inoherb.commands.emulsifier">
      </command>
      <command
            name="香精类"
            categoryId="com.uds.inoherb.commands.category"
            id="com.uds.inoherb.commands.parfum">
      </command>
      <command
            name="活性物类"
            categoryId="com.uds.inoherb.commands.category"
            id="com.uds.inoherb.commands.actives">
      </command>
   </extension>

   <extension
         point="org.eclipse.ui.handlers">
      <handler
            commandId="com.uds.inoherb.commands.regulatoryStandards"
            class="com.uds.inoherb.handlers.SampleHandler">
      </handler>
      <handler
            commandId="com.uds.inoherb.commands.rawMaterial"
            class="com.uds.inoherb.handlers.SampleHandler">
      </handler>
      <handler
            commandId="com.uds.inoherb.commands.productStandard"
            class="com.uds.inoherb.handlers.SampleHandler">
      </handler>
      <handler
            commandId="com.uds.inoherb.commands.fadAndOil"
            class="com.uds.inoherb.handlers.SampleHandler">
      </handler>
   </extension>

   <extension
         point="org.eclipse.ui.bindings">
      <key
            commandId="com.uds.inoherb.commands.sampleCommand"
            contextId="org.eclipse.ui.contexts.window"
            sequence="M1+6"
            schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
      </key>
   </extension>

   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="menu:org.eclipse.ui.main.menu?after=additions">
         <menu
               label="企业电子数据库"
               id="com.uds.inoherb.menus.sampleMenu">

               <menu 
               label="法规标准库"
               id="com.uds.inoherb.menus.sampleMenu2">
               <command
                  commandId="com.uds.inoherb.commands.productStandard"
                  id="com.uds.inoherb.menus.productStandard">
               </command>
               <command
                  commandId="com.uds.inoherb.commands.prohibitedList"
                  id="com.uds.inoherb.menus.prohibitedList">
               </command>
               <command
                  commandId="com.uds.inoherb.commands.fullComposition"
                  id="com.uds.inoherb.menus.fullComposition">
               </command>
               </menu>

               <menu 
               label="原料库"
               id="com.uds.inoherb.menus.sampleMenu3"> 
               <command
                  commandId="com.uds.inoherb.commands.fadAndOil"
                  id="com.uds.inoherb.menus.fadAndOil">
               </command>
               <command
                  commandId="com.uds.inoherb.commands.emulsifier"
                  id="com.uds.inoherb.menus.emulsifier">
               </command>
               <command
                  commandId="com.uds.inoherb.commands.parfum"
                  id="com.uds.inoherb.menus.parfum">
               </command>
               <command
                  commandId="com.uds.inoherb.commands.actives"
                  id="com.uds.inoherb.menus.actives">
               </command>
               </menu>
         </menu>
      </menuContribution>
   </extension>
</plugin>

  • 写回答

3条回答

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型