dom4j的读写XML文件

java用dom4j读写xml很方便,工作之余,做了一点小总结。xml文件格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
     <dbip port="3306">127.0.0.1</dbip>
</Root>

1、读取dbip节点的值“127.0.0.1”和port的值“3306”.

    /**
     * 获取IP和port的值
     * @throws Exception
     */
    public void showIpPort() throws Exception{
        SAXReader reader = new SAXReader();
        Document doc = reader.read(new File("D:\\aa.xml"));
        String ip = doc.getRootElement().elementText("dbip");
        String port = doc.getRootElement().element("dbip").attributeValue("port");
        //将结果打印出来
        System.out.println(ip+"==="+port);
    }

 

2、添加dbip节点。

    /**
     * 添加一个dbip节点
     * @throws Exception
     */
    public void addNode() throws Exception{
        SAXReader read = new SAXReader();
        File f = new File("D:\\aa.xml");
        Document doc = read.read(new File("D:\\aa.xml"));
        Element root = doc.getRootElement();
        Element dbip = root.addElement("dbip");
        dbip.setText("192.168.1.201");
        dbip.addAttribute("port","8080");
        
        //修改
        XMLWriter output = new XMLWriter(new FileWriter(f));
        output.write(doc);
        output.close();
    }

添加后的xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <dbip port="3306">127.0.0.1</dbip>
    <dbip port="8080">192.168.1.201</dbip>
</Root>

 

3、修改port为8080的节点dbip的port的值为8088,值为192.168.1.202.

    /**
     * 修改port为8080的节点dbip的port的值为8088,值为192.168.1.202
     * @throws Exception
     */
    public void upNode() throws Exception{
        SAXReader read = new SAXReader();
        File f = new File("D:\\aa.xml");
        Document doc = read.read(new File("D:\\aa.xml"));
        Element root = doc.getRootElement();
        List<Element> list = root.elements("dbip");
        for(Element dbip:list){
            if(dbip.attributeValue("port").equals("8080")){
                dbip.setText("192.168.1.202");
                dbip.attribute("port").setValue("8088");
                
            }
        }
        
        //修改
        XMLWriter output = new XMLWriter(new FileWriter(f));
        output.write(doc);
        output.close();
        System.out.println("修改完毕");
    }

修改后的XML如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <dbip port="3306">127.0.0.1</dbip>
    <dbip port="8088">192.168.1.202</dbip>
</Root>

 

4、删除port为8088的dbip节点.

    /**
     * 删除port为8088的dbip节点
     * @throws Exception
     */
    public void delNode() throws Exception{
        SAXReader read = new SAXReader();
        File f = new File("D:\\aa.xml");
        Document doc = read.read(f);
        Element root = doc.getRootElement();
        List<Element> list = root.elements("dbip");
        
        for(int i=0;i<list.size();i++){
            Element dbip = list.get(i);
            if(dbip.attributeValue("port").equals("8088")){
                list.remove(i);
            }
        }
        //这里不能用以下这种方式,否则会报一个下面的错误
        //java.util.ConcurrentModificationException
//        for(Element dbip:list){
//            if(dbip.attributeValue("port").equals("8088")){
//                list.remove(dbip);
//            }
//        }
        
        //修改
        XMLWriter output = new XMLWriter(new FileWriter(f));
        output.write(doc);
        output.close();
        System.out.println("删除完毕");
    }

删除后的XML如图所示:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <dbip port="3306">127.0.0.1</dbip>
</Root>

 

posted on 2013-05-03 14:04  过省  阅读(262)  评论(0编辑  收藏  举报

导航