码迷,mamicode.com
首页 > 其他好文 > 详细

Dom4j操作XML文件

时间:2016-06-29 18:33:15      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

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>

Dom4j操作XML文件

标签:

原文地址:http://www.cnblogs.com/mstk/p/5627729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!