本节的主要内容在理解xml的解析方式以及dom4j节点查/增删改的方法运用, 最后学习了xpath定位方法.
解析
dom解析: 一次读入xml加载返回document对象, 然后增删改查
sax解析: 读一行解析一行, 查询效率高, 不能改
package com.wang.xml;
import org.dom4j.*;
import org.dom4j.io.*;
import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName TestXmlParser
* @Description TODO
* @Author Yeseung
* @Date 2021/5/129:58
* @Version 1.0
**/
public class TestXmlParser {
@Test
public void test() throws DocumentException {
//1. 导入dom解析包
//2. 创建saxRedaer
SAXReader reader = new SAXReader();
//3. saxReader读取文件, 返回一个document对象
Document document = reader.read(new File("books.xml"));
System.out.println(document);
//4. 获取到后增删改查
}
@Test
public void testxml2() throws DocumentException {
//1. 导包; 2. 创建一个阅读器;
SAXReader reader = new SAXReader();
//3. 使用阅读器读取文件
Document document = reader.read(new File("books.xml"));
//解析
// 返回9
System.out.println(document.getNodeType());
//先要获取根节点, 使用根节点向下找
Element rootElement = document.getRootElement();
//节点类型
System.out.println(rootElement.getNodeType());
//节点名称
System.out.println(rootElement.getName());
//当前节点下所有子节点
List<Element> list = rootElement.elements();
for (Element e : list) {
//获取当前节点下, 名为name的元素的文本值
System.out.println(e.elementText("name"));
List<Element> list1 = e.elements();
for (Element ele : list1) {
// 获取本标签中的文本值
System.out.println(ele.getText());
}
}
// list中是所有book集合
for (Element e : list) {
System.out.println(e.attributeValue("sn"));
}
}
@Test
public void testPojo() throws Exception {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("books.xml"));
Element rootElement = document.getRootElement();
List<Element> list = rootElement.elements();
List<Book> bookList = new ArrayList<>();
for (Element e : list) {
String sn = e.attributeValue("sn");
String name = e.elementText("name");
String price = e.elementText("price");
String author = e.elementText("author");
bookList.add(new Book(name, price, author, sn));
}
System.out.println(bookList);
}
@Test
public void testChangeSave() throws Exception {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("books.xml"));
Element rootElement = document.getRootElement();
//获取第一个element
Element book = rootElement.element("book");
Element name = book.element("name");
//修改文本值和属性
name.setText("我想试试!");
name.addAttribute("more-information", "test");
//把修改的东西保存起来, OutputFormat输出的数据格式化
// OutputFormat opf = OutputFormat.createCompactFormat();
OutputFormat opf = OutputFormat.createPrettyPrint();
//XMLWriter用来写对象
XMLWriter writer = new XMLWriter(new FileOutputStream("output.xml"), opf);
writer.write(document);
writer.close();
}
@Test
public void testXpath() throws Exception{
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("books.xml"));
Element rootElement = document.getRootElement();
// xpath获取元素, 依据sn
Node node = rootElement.selectSingleNode("//book[@sn='SN12341232']");
Element e1 = (Element) node;
System.out.println(e1.attributeValue("sn"));
//xpath获取到所有name标签
List<Element> list = rootElement.selectNodes("//name");
for (Element e: list){
System.out.println(e.getText());
}
}
}
相应的xml:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book sn="SN12341232">
<name>辟邪剑谱</name>
<price>9.9</price>
<author>班主任</author>
</book>
<book sn="SN12341231">
<name>葵花宝典</name>
<price>99.99</price>
<author>班长</author>
</book>
</books>