亲爱的耀哥哥 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条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面