Java dom4j生成与解析XML案例详解

时间:2022-08-30 22:10:28

一)dom4j介绍

使用方式:在pom.xml中导入dom4j对应的jar

?
1
2
3
4
5
<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
</dependency>

优点:dom4j使Java生成和解析XML灵活性变高,并且代码易于维护

API操作类:

  • Document:表示xml文档信息,是一个树形结构
  • Eelment:表示xml的元素结点,提供一些操作其子元素方法的,如文本、属性、名称空间等
  • Attribute:表示元素结点中的属性

二)dom4j生成xml

实现步骤:

第一步:创建一个Document实例

Document doc = DocumentHelper.createDocument();

第二步:先添加一个根结点,然后再添加子结点,构造成一个树形结构

Element root = doc.addElement("root");

第三步:添加xml文件样式(也可自定义样式),并输出xml文件到指定的路径下

OutputFormat format = OutputFormat.createPrettyPrint();

XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
writer.write(doc);

实现源码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.oysept.xml;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
 
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.jdom2.output.Format;
 
/**
 * dom4j生成xml
 * @author ouyangjun
 */
public class CreateDom4j {
 
    public static void main(String[] args) {
        // 执行dom4j生成xml方法
        createDom4j(new File("E:\\dom4j.xml"));
    }
    
    public static void createDom4j(File file) {
        try {
            // 创建一个Document实例
            Document doc = DocumentHelper.createDocument();
            
            // 添加根节点
            Element root = doc.addElement("root");
            
            // 在根节点下添加第一个子节点
            Element oneChildElement= root.addElement("person").addAttribute("attr", "root noe");
            oneChildElement.addElement("people")
                    .addAttribute("attr", "child one")
                    .addText("person one child one");
            oneChildElement.addElement("people")
                    .addAttribute("attr", "child two")
                    .addText("person one child two");
            
            // 在根节点下添加第一个子节点
            Element twoChildElement= root.addElement("person").addAttribute("attr", "root two");
            twoChildElement.addElement("people")
                    .addAttribute("attr", "child one")
                    .addText("person two child one");
            twoChildElement.addElement("people")
                    .addAttribute("attr", "child two")
                    .addText("person two child two");
            
            // xml格式化样式
            // OutputFormat format = OutputFormat.createPrettyPrint(); // 默认样式
            
            // 自定义xml样式
            OutputFormat format = new OutputFormat();
            format.setIndentSize(2);  // 行缩进
            format.setNewlines(true); // 一个结点为一行
            format.setTrimText(true); // 去重空格
            format.setPadText(true);
            format.setNewLineAfterDeclaration(false); // 放置xml文件中第二行为空白行
            
            // 输出xml文件
            XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
            writer.write(doc);
            System.out.println("dom4j CreateDom4j success!");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

XML文件效果图:

Java dom4j生成与解析XML案例详解

三)dom4j解析xml

实现步骤:

第一步:创建一个SAXReader解析器

SAXReader reader = new SAXReader();

第二步:解析xml文件,重新构建成一个Document对象

Document doc = reader.read(file);

第三步:处理Document对象信息,在控制台打印

实现源码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.oysept.xml;
 
import java.io.File;
import java.util.Iterator;
import java.util.List;
 
import org.dom4j.Attribute;
import org.dom4j.CDATA;
import org.dom4j.Comment;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.Text;
import org.dom4j.io.SAXReader;
 
/**
 * dom4j解析xml
 * @author ouyangjun
 */
public class ParseDom4j {
 
    public static void main(String[] args) {
        // 执行dom4j解析xml方法
        parseDom4j(new File("E:\\dom4j.xml"));
    }
    
    public static void parseDom4j(File file) {
        try {
            // 创建一个SAXReader解析器
            SAXReader reader = new SAXReader();
            
            // 读取xml文件,转换成Document结点
            Document doc = reader.read(file);
 
            // 递归打印xml文档信息
            StringBuffer buffer = new StringBuffer();
            parseElement(doc.getRootElement(), buffer);
            System.out.println(buffer.toString());
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
    
    @SuppressWarnings("unchecked")
    public static void parseElement(Element element, StringBuffer buffer) {
        buffer.append("<"+element.getName());
        List<Attribute> attrs = element.attributes();
        if (attrs != null) {
            for (Attribute attr : attrs) {
                buffer.append(" "+attr.getName()+"=\""+attr.getValue()+"\"");
            }
        }
        buffer.append(">");
        
        Iterator<Node> iterator = element.nodeIterator();
        while (iterator.hasNext()) {
            Node node = iterator.next();
            if (node instanceof Element) {
                Element eleNode = (Element) node;
                parseElement(eleNode, buffer);
            }
            if (node instanceof Text) {
                Text text = (Text) node;
                buffer.append(text.getText());
            }
            if (node instanceof CDATA) {
                CDATA dataNode = (CDATA) node;
                buffer.append(dataNode.getText());
            }
            if (node instanceof Comment) {
                Comment comNode = (Comment) node;
                buffer.append(comNode.getText());
            }
        }
        buffer.append("</"+element.getName()+">");
    }
}

XML文件解析效果图打印:

Java dom4j生成与解析XML案例详解

到此这篇关于Java dom4j生成与解析XML案例详解的文章就介绍到这了,更多相关Java dom4j生成与解析XML内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/p812438109/article/details/81814157