程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> Dom4j操作XML文件,dom4j操作xml

Dom4j操作XML文件,dom4j操作xml

編輯:JAVA綜合教程

Dom4j操作XML文件,dom4j操作xml


1. 創建並寫入XML

    public static void writeXML(String filename){
        try {
            Document document = DocumentHelper.createDocument();
            Element root = document.addElement("PortConfig");             
            Element ePort;
             
            ePort = root.addElement("Port");
            ePort.addAttribute("Portname", "portBloodpress");
            ePort.addAttribute("Portnumber", "COM12");
             
            ePort = root.addElement("Port");
            ePort.addAttribute("Portname", "portWah");
            ePort.addAttribute("Portnumber", "COM14");
             
            ePort = root.addElement("Port");
            ePort.addAttribute("Portname", "portBloodsugar");
            ePort.addAttribute("Portnumber", "COM16");
             
            ePort = root.addElement("Port");
            ePort.addAttribute("Portname", "portBloodfat");
            ePort.addAttribute("Portnumber", "COM18");
             
            ePort = root.addElement("Port");
            ePort.addAttribute("Portname", "portUric");
            ePort.addAttribute("Portnumber", "COM20");
             
            OutputFormat format = new OutputFormat("    ", true);
            XMLWriter output = new XMLWriter(new FileWriter(new File(filename)), format);
            output.write(document);
            output.close();
             
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }

首先用DocumentHelper創建一個Document對象,然後給Document添加一個Element,也就是根Element;然後可以在這個根Element下面添加若干個Element,每一個Element用addAttribute()添加了屬性名稱和屬性值;最後,使用XMLWriter把Document寫入文件。

2. 讀取並解析XML

    public static void readXML(String filename){
         
        try {              
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(filename);
            // 獲取根節點
            Element portConfig = document.getRootElement();
            for (Iterator i = portConfig.elementIterator(); i.hasNext();) {  
                Element port = (Element) i.next();
                String aname = port.attributeValue("Portname");
                String avalue = port.attributeValue("Portnumber");
                if (aname.equals("portBloodpress")){
                    Constant.portBloodpress = avalue;
                }
                else if (aname.equals("portWah")){
                    Constant.portWah = avalue;
                }
                else if (aname.equals("portBloodsugar")){
                    Constant.portBloodsugar = avalue;
                }
                else if (aname.equals("portBloodfat")){
                    Constant.portBloodfat = avalue;
                }
                else if (aname.equals("portUric")){
                    Constant.portUric = avalue;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

首先創建一個SAXReader,用這個SAXReader讀出一個Document;然後獲得這個Document的根Element,迭代獲得根Element下面的每一個Element,對每一個Element,使用attributeValue()獲得它的屬性值。

3. 更新XML

    public static void updateXML(String filename){
        try {              
            File f = new File(filename);
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(curDir + "\\port_config.xml");
            // 獲取根節點
            Element portConfig = document.getRootElement();
            for (Iterator i = portConfig.elementIterator(); i.hasNext();) {  
                Element port = (Element) i.next();
                String aname = port.attributeValue("Portname");
                if (aname.equals("portBloodpress")){
                    port.setAttributeValue("Portnumber", Constant.portBloodpress);
                }
                else if (aname.equals("portWah")){
                    port.setAttributeValue("Portnumber", Constant.portWah);
                }
                else if (aname.equals("portBloodsugar")){
                    port.setAttributeValue("Portnumber", Constant.portBloodsugar);
                }
                else if (aname.equals("portBloodfat")){
                    port.setAttributeValue("Portnumber", Constant.portBloodfat);
                }
                else if (aname.equals("portUric")){
                    port.setAttributeValue("Portnumber", Constant.portUric);
                }
            }
            XMLWriter output = new XMLWriter(new FileWriter(new File(curDir + "\\port_config.xml")));
            output.write(document);
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }        
    }

更新和讀取的過程差不多,只是對需要更新的屬性用setAttributeValue()方法更新,最後再用XMLWriter寫入文件。

XML文件:

<?xml version="1.0" encoding="UTF-8"?>
<PortConfig>
    <Port Portname="portBloodpress" Portnumber="COM126"/>
    <Port Portname="portWah" Portnumber="COM146"/>
    <Port Portname="portBloodsugar" Portnumber="COM166"/>
    <Port Portname="portBloodfat" Portnumber="COM186"/>
    <Port Portname="portUric" Portnumber="COM206"/>
</PortConfig>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved