yyqkn2015
亲爱的耀哥哥
采纳率75%
2016-09-01 09:03

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

50

做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条回答

  • Veggiel Veggiel 5年前

    xml文件读取

     import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    
    import excel.Student;
    
    public class Js {
        public static void main(String[] args) {
            // 读取创建file对象
            File file = new File("F://data.xml");
            SAXReader reader = new SAXReader();
            Document xmlDoc = null;
            try {
                 //创建Document对象
                xmlDoc = reader.read(file);
            } catch (DocumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Element root = xmlDoc.getRootElement();
            List<Element> data = root.elements();
            List<Student> list = new ArrayList<Student>();
            for (int i = 0; i < data.size(); i++) {
                Student student = new Student(data.get(i).elementText("name"),
                        Integer.parseInt(data.get(i).elementText("id")));
                list.add(student);
            }
            System.out.println(list);
        }
    }
    
    

    xml文件写入

     import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Text;
    
    public class CreateXML {
    public static void main(String[] args) throws Exception {
      //实例化解析器
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
       DocumentBuilder builder = factory.newDocumentBuilder();
        //创建Document对象
       Document doc = builder.newDocument();
    
       //创建XML文件所需的各种对象并序列化
       Element data = doc.createElement("data");
       Element student = doc.createElement("student");
       Element name = doc.createElement("name");
       Element id = doc.createElement("id");
       Text nameText = doc.createTextNode("张三");
       Text idText    = doc.createTextNode("123456");
       doc.appendChild(data);
       data.appendChild(student);
       student.appendChild(name);
       student.appendChild(id); 
       name.appendChild(nameText);
       id.appendChild(idText);
       student.setAttribute("isbn", "00001");
       doc2XmlFile(doc,"F://test.xml");
    
    }
    
    public static boolean doc2XmlFile(Document document, String filename) {
       boolean flag = true;
       try {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UFT-8");
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(new File(filename));
        transformer.transform(source, result);
       } catch (Exception ex) {
        flag = false;
        ex.printStackTrace();
       }
       return flag;
    }
    }
    
    

    data.dtd

     <?xml version="1.0" encoding="UTF-8"?>
    <!ELEMENT data (student+)>
    <!ELEMENT student (name,id)>
    <!ELEMENT name (#PCDATA)>
    <!ELEMENT id (#PCDATA)>
    <!ATTLIST student isbn ID #REQUIRED aaa CDATA #IMPLIED>
    

    data.xml

     <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE note SYSTEM "data.dtd">
    
    <data>
        <student isbn="A001">
            <name>张三</name>
            <id>1234567</id>
        </student>
    </data>
    
    点赞 1 评论 复制链接分享
  • Marksinoberg 郭 璞 5年前

    XML小试

    XML应用

    这上面的两个小例子应该对你这个需求很有帮助,代码注释也非常清晰,希望对你有所帮助 :-)

    点赞 1 评论 复制链接分享
  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 5年前