利用dom4j解析spring.xml

<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">








1)自己创建Document对象
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
Document document = DocumentHelper.createDocument();

Element root = document.addElement("students");

其中students是根节点,可以继续添加其他节点等操作。
(2)读取XML文件获取Document对象
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
//创建SAXReader对象

SAXReader reader = new SAXReader();

//读取文件 转换成Document

Document document = reader.read(new File("XXXX.xml"));

(3)读取XML文本内容获取Document对象
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
String xmlStr = "......";

Document document = DocumentHelper.parseText(xmlStr);

3、示例
(1)xml文件内容如下
[html] view plain copy print?在CODE上查看代码片派生到我的代码片
<?xml version="1.0" encoding="UTF-8"?>





<微信公众号>@残缺的孤独</微信公众号>

<学号>20140101</学号>

<地址>北京海淀区</地址>

<座右铭>要么强大,要么听话</座右铭>





<新浪微博>@残缺的孤独</新浪微博>

<学号>20140102</学号>

<地址>北京朝阳区</地址>

<座右铭>在哭泣中学会坚强</座右铭>





(2)解析过程
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
package cn.com.yy.dom4j;

import java.io.File;

import java.util.Iterator;

import java.util.List;

import org.dom4j.Attribute;

import org.dom4j.Document;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;

import org.junit.Test;

public class Dom4JforXML {

@Test  
public void test() throws Exception{  
    //创建SAXReader对象  
    SAXReader reader = new SAXReader();  
    //读取文件 转换成Document  
    Document document = reader.read(new File("src/cn/com/yy/dom4j/s.xml"));  
    //获取根节点元素对象  
    Element root = document.getRootElement();  
    //遍历  
    listNodes(root);  
}  

//遍历当前节点下的所有节点  
public void listNodes(Element node){  
    System.out.println("当前节点的名称:" + node.getName());  
    //首先获取当前节点的所有属性节点  
    List<Attribute> list = node.attributes();  
    //遍历属性节点  
    for(Attribute attribute : list){  
        System.out.println("属性"+attribute.getName() +":" + attribute.getValue());  
    }  
    //如果当前节点内容不为空,则输出  
    if(!(node.getTextTrim().equals(""))){  
         System.out.println( node.getName() + ":" + node.getText());    
    }  
    //同时迭代当前节点下面的所有子节点  
    //使用递归  
    Iterator<Element> iterator = node.elementIterator();  
    while(iterator.hasNext()){  
        Element e = iterator.next();  
        listNodes(e);  
    }  
}  

}

(3)解析结果
[html] view plain copy print?在CODE上查看代码片派生到我的代码片
当前节点的名称:students

当前节点的名称:student1

属性id:001

当前节点的名称:微信公众号

微信公众号:@残缺的孤独

当前节点的名称:学号

学号:20140101

当前节点的名称:地址

地址:北京海淀区

当前节点的名称:座右铭

座右铭:要么强大,要么听话

当前节点的名称:student2

属性id:002

当前节点的名称:新浪微博

新浪微博:@残缺的孤独

当前节点的名称:学号

学号:20140102

当前节点的名称:地址

地址:北京朝阳区

当前节点的名称:座右铭

座右铭:在哭泣中学会坚强

4、dom4j操作节点属性
使用dom4j可以操作节点属性,比如添加节点属性、删除节点属性、修改属性值等操作。下面使用dom4j为上述的student1节点删除id属性,新添name属性。
(1)代码示例
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
@Test

public void test2()throws Exception{

//创建SAXReader对象

SAXReader reader = new SAXReader();

//读取文件 转换成Document

Document document = reader.read(new File("src/cn/com/yy/dom4j/s.xml"));

//获取根节点元素对象

Element root = document.getRootElement();

    System.out.println("-------添加属性前------");  
    //获取节点student1  
    Element student1Element = root.element("student1");  
    //遍历  
    listNodes(student1Element);  
    //获取其属性  
    Attribute idAttribute = student1Element.attribute("id");  
    //删除其属性  
    student1Element.remove(idAttribute);  
    //为其添加新属性  
    student1Element.addAttribute("name", "这是student1节点的新属性");  
    System.out.println("-------添加属性后------");  
    listNodes(student1Element);  
}  

(2)结果
[html] view plain copy print?在CODE上查看代码片派生到我的代码片
-------添加属性前------

当前节点的名称:student1

属性id:001

当前节点的名称:微信公众号

微信公众号:@残缺的孤独

当前节点的名称:学号

学号:20140101

当前节点的名称:地址

地址:北京海淀区

当前节点的名称:座右铭

座右铭:要么强大,要么听话

-------添加属性后------

当前节点的名称:student1

属性name:这是student1节点的新属性

当前节点的名称:微信公众号

微信公众号:@残缺的孤独

当前节点的名称:学号

学号:20140101

当前节点的名称:地址

地址:北京海淀区

当前节点的名称:座右铭

座右铭:要么强大,要么听话

5、dom4j新增节点
使用dom4j可以删除指定节点、新增节点等操作,我们使用dom4j为student1节点新增phone节点,如下。
(1)代码
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
//添加节点

@Test

public void test3()throws Exception{

//创建SAXReader对象

SAXReader reader = new SAXReader();

//读取文件 转换成Document

Document document = reader.read(new File("src/cn/com/yy/dom4j/s.xml"));

//获取根节点元素对象

Element root = document.getRootElement();

System.out.println("-------添加节点前------");

//获取节点student1

Element student1Element = root.element("student1");

//遍历

listNodes(student1Element);

//添加phone节点

Element phoneElement = student1Element.addElement("phone");

//为phone节点设置值

phoneElement.setText("137xxxxxxxx");

System.out.println("-------添加节点后------");

listNodes(student1Element);

}

(2)结果
[html] view plain copy print?在CODE上查看代码片派生到我的代码片
-------添加节点前------

当前节点的名称:student1

属性id:001

当前节点的名称:微信公众号

微信公众号:@残缺的孤独

当前节点的名称:学号

学号:20140101

当前节点的名称:地址

地址:北京海淀区

当前节点的名称:座右铭

座右铭:要么强大,要么听话

-------添加节点后------

当前节点的名称:student1

属性id:001

当前节点的名称:微信公众号

微信公众号:@残缺的孤独

当前节点的名称:学号

学号:20140101

当前节点的名称:地址

地址:北京海淀区

当前节点的名称:座右铭

座右铭:要么强大,要么听话

当前节点的名称:phone

phone:137xxxxxxxx

6、把Document对象写入新的文件
有时,我们需要把document对象写入新的文件,dom4j提供了对应的API以便我们进行操作。我们在完成第 5 后,把document写入新的文件s1.xml,如下。
(1)代码
[java] view plain copy print?在CODE上查看代码片派生到我的代码片
//添加节点后,写入新的文件

@Test

public void test4()throws Exception{

//创建SAXReader对象

SAXReader reader = new SAXReader();

//读取文件 转换成Document

Document document = reader.read(new File("src/cn/com/yy/dom4j/s.xml"));

//获取根节点元素对象

Element root = document.getRootElement();

System.out.println("-------添加节点前------");

//获取节点student1

Element student1Element = root.element("student1");

//遍历

listNodes(student1Element);

//添加phone节点

Element phoneElement = student1Element.addElement("phone");

//为phone节点设置值

phoneElement.setText("137xxxxxxxx");

System.out.println("-------添加节点后------");

listNodes(student1Element);

//把student1Element写入新文件

writerDocumentToNewFile(document);

System.out.println("---写入完毕----");

}

//document写入新的文件  
public void writerDocumentToNewFile(Document document)throws Exception{  
    //输出格式  
    OutputFormat format = OutputFormat.createPrettyPrint();  
    //设置编码  
    format.setEncoding("UTF-8");  
    //XMLWriter 指定输出文件以及格式  
    XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File("src/cn/com/yy/dom4j/s1.xml")),"UTF-8"), format);  

    //写入新文件  
    writer.write(document);  
    writer.flush();  
    writer.close();  
}  

(2)查看s1.xml文件
[html] view plain copy print?在CODE上查看代码片派生到我的代码片
<?xml version="1.0" encoding="UTF-8"?>





<微信公众号>@残缺的孤独</微信公众号>

<学号>20140101</学号>

<地址>北京海淀区</地址>

<座右铭>要么强大,要么听话</座右铭>

137xxxxxxxx





<新浪微博>@残缺的孤独</新浪微博>

<学号>20140102</学号>

<地址>北京朝阳区</地址>

<座右铭>在哭泣中学会坚强</座右铭>



因为涉及到中文,所以在输出时要设定UTF8编码,OutputStreamWriter进行设置编码。
还有输出格式的问题,在此处使用的是OutputFormat.createPrettyPrint(),输出文档时进行了排版格式化。还有一种是OutputFormat.createCompactFormat()方法,输出内容是一行,没有进行格式化,是紧凑型的输出。如下:
[html] view plain copy print?在CODE上查看代码片派生到我的代码片
<?xml version="1.0" encoding="UTF-8"?>

<微信公众号>@残缺的孤独</微信公众号><学号>20140101</学号><地址>北京海淀区</地址><座右铭>要么强大,要么听话</座右铭>137xxxxxxxx<新浪微博>@残缺的孤独</新浪微博><学号>20140102</学号><地址>北京朝阳区</地址><座右铭>在哭泣中学会坚强</座右铭>