XML文件 books2.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>
创建一个Book类如下:
成员变量对应XML文件属性、子标签
package com.atguigu.pojo;
import java.math.BigDecimal;
/*
*
*
*
*
*@author 刘鑫
*@create by 2021/4/25 18:40
*/
public class Book {
private String sn;
private String name;
private Double price;
private String author;
public Book() {
}
public Book(String sn, String name, Double price, String author) {
this.sn = sn;
this.name = name;
this.price = price;
this.author = author;
}
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"sn='" + sn + '\'' +
", name='" + name + '\'' +
", price=" + price +
", author='" + author + '\'' +
'}';
}
}
测试如下:
需要导入Junit依赖和dom4j依赖
@Test
public void text1() throws Exception {
//创建一个SAXReader输入流,去读取xml配置文件,生成Document对象
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("src/books2.xml");
System.out.println(document);
}
知识点:
1、需要创建SAXReader输入流对象,去读取xml配置文件,生成Document对象。
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("Xml path");
/*
* 读取books2.xml文件生成Book类
* */
@Test
public void test2() throws DocumentException {
//1 读取books.xml文件
SAXReader saxReader = new SAXReader();
Document document = saxReader.read("src/books2.xml");
//2 通过Document对象获取根元素
Element rootElement = document.getRootElement();
//System.out.println(rootElement);
//3 通过根元素获取book标签对象
//element()和elements()都是通过标签名查找子元素
List<Element> books = rootElement.elements("book");
//4 遍历,处理每个book标签转换成Book类
for (Element book : books) {
//asXML把标签对象转换成标签字符串
//System.out.println(book.asXML());
//getText():可以获取标签中的文本内容
Element nameElement = book.element("name");
String nameText = nameElement.getText();
//System.out.println(nameText);
//直接获取指定子标签名的文本内容
String priceText = book.elementText("price");
//System.out.println(price);
String authorText = book.elementText("author");
//attribute();通过属性名获取属性
String snValue = book.attributeValue("sn");
System.out.println(new Book(snValue,nameText,Double.parseDouble(priceText),authorText));
}
}
知识点:
2、通过Document对象获取根元素:getRootElement()
Element rootElement = document.getRootElement();
3、通过根元素获取book标签对象:element() 和 elements() 都是通过标签名查找子元素
List<Element> books = rootElement.elements("book");
4、asXML() 把标签对象转换成标签字符串
5、getText() 可以获取标签中的文本内容
6、elementText(“price”) 直接通过子标签名获取该子元素标签中的文本内容(直接获取指定子标签名的文本内容)
7、attributeValue(“sn”) 通过属性名获取属性
test2()最终打印结果:
Book{sn='SN12341232', name='辟邪剑谱', price=9.9, author='班主任'}
Book{sn='SN12341231', name='葵花宝典', price=99.99, author='班长'}
故根据xml文件创建出了两个Book对象。