[XML] to read and write XML documents using the XML document Dom4j

About Dom4j

dom4j is a Java XML API, is jdom upgrade products, used to read and write XML documents. dom4j is a very good the Java XML API, with excellent performance, powerful and extremely easy-to-use features, its performance over the sun's official dom technology, software, and it is also an open source, it can be found on SourceForge . You can also find an article on IBM developerWorks above, to be mainstream Java XML API performance, functionality and ease of evaluation, it is possible to know dom4j no matter which side are very good. Now we can see more and more Java software are in use dom4j to read and write XML, is particularly worth mentioning is that even the Sun is also used JAXM dom4j. This was the jar package must be used, Hibernate also use it to read and write configuration files.

dom4j Configuration

  • Official website to download dom4j
  • In a project to create a new Folder to add dom4j
  • Click the right mouse button to find the Build Path added to the items on the dom4j

Read XML documents using the Dom4j

In my blog post [XML] the basic structure of XML and XML-Schema constraints of XML documents, for example:

import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class HrReader {
    
    public void readXml(){
        String file = "E:/workspace/eclipse/HelloWorld/src/test2/hr.xml";
        //SAXReader类是读取XML文件的核心类,用于将XML解析后以“树”的形式保存在内存中
        SAXReader reader = new SAXReader(); 
        try {
            Document document = reader.read(file);
            Element root = document.getRootElement();//获取XML文档的根节点,即hr标签
            List<Element> employees = root.elements("employee");//elements方法用于获取指定的标签集合
            for(Element employee : employees){
                Element name = employee.element("name");//element方法用于获取唯一的子节点对象
                String empName = name.getText();//getText()方法用于获取标签文本
                System.out.println(empName);
                System.out.println(employee.elementText("age"));
                System.out.println(employee.elementText("salary"));
                Element department = employee.element("department");
                System.out.println(department.element("dname").getText());
                System.out.println(department.elementText("address"));
                Attribute att = employee.attribute("no");
                System.out.println(att.getText());
                System.out.println("------");
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        HrReader reader = new HrReader();
        reader.readXml();
    }
}

Use Dom4j write XML documents

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class HrWriter {
    
    public void writeXml(){
        String file = "E:/workspace/eclipse/HelloWorld/src/test2/hr.xml";//XML文件地址
        SAXReader reader = new SAXReader();//SAXReader类是读取XML文件的核心类
        try {
            Document document = reader.read(file);//将XML解析后以“树”的形式保存在内存中
            Element root = document.getRootElement();//获取XML文档的根节点,即hr标签
            Element employee = root.addElement("employee");//加入一个员工
            employee.addAttribute("no", "009");//设置员工属性
            Element name = employee.addElement("name");//加入一个员工名称
            name.setText("李四");//设置员工名称
            employee.addElement("age").setText("34");//加入并设置员工的年龄
            employee.addElement("salary").setText("6000");//加入并设置员工的薪水
            Element department = employee.addElement("department");//加入员工所属部门
            department.addElement("dname").setText("人事部");//加入并设置员工所属部门的名字
            department.addElement("address").setText("XX大厦-B105");//加入并设置员工所属部门的地址
            Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");//将文件输出流转化成writer对象
            document.write(writer);//将内存中构建的DOM模型写入XML文件中
            writer.close();//关闭输出流
        } catch (Exception e) {
            e.printStackTrace();//如果有异常则打印堆栈信息
        }
    }
    
    public static void main(String[] args){
        HrWriter hrWriter = new HrWriter();//新建一个hrWriter对象
        hrWriter.writeXml();//调用writeXml方法
    }
}

After the XML document is written as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 人力资源管理系统 -->
<hr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="hr.xsd">
    <employee no="007">
        <name>或无言</name>
        <age>18</age>
        <salary>20000</salary>
        <department>
            <dname>开发部</dname>
            <address>XX大厦-B103</address>
        </department>
    </employee>
    <employee no="008">
        <name>张三</name>
        <age>31</age>
        <salary>30000</salary>
        <department>
            <dname>工程部</dname>
            <address>XX大厦-B104</address>
        </department>
    </employee>
    <employee no="009">
        <name>李四</name>
        <age>34</age>
        <salary>6000</salary>
        <department>
            <dname>人事部</dname>
            <address>XX大厦-B105</address>
        </department>
    </employee>
</hr>

to sum up

dom4j is a good tool, when a large number of XML document data to be processed is very convenient.

references:

https://www.baidu.com/s?wd=dom4j&ie=UTF-8

Guess you like

Origin www.cnblogs.com/huowuyan/p/11203846.html