实体类
package com;
import java.math.BigDecimal;
import java.util.Objects;
/**
* @author hrui
* @date 2023/10/20 16:39
*/
public class Book {
private String sn;
private String name;
private BigDecimal price;
private String author;
public Book() {
}
public Book(String sn, String name, BigDecimal price, String author) {
this.sn = sn;
this.name = name;
this.price = price;
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"sn='" + sn + '\'' +
", name='" + name + '\'' +
", price=" + price +
", author='" + author + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return Objects.equals(sn, book.sn) && Objects.equals(name, book.name) && Objects.equals(price, book.price) && Objects.equals(author, book.author);
}
@Override
public int hashCode() {
return Objects.hash(sn, name, price, 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 BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
package com;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
* @author hrui
* @date 2023/10/20 16:42
*/
public class Dom4jTest {
@Test
public void test01(){
//创建一个SaxReader输入流,去读取xl配置文件,生成Document对象
SAXReader saxReader=new SAXReader();
Document document= null;
try {
document = saxReader.read("src/books.xml");
} catch ( Exception e) {
e.printStackTrace();
}
System.out.println(document);
}
//读取books.xml文件生成Book类
@Test
public void test02() throws DocumentException {
String projectRoot = System.getProperty("user.dir");
System.out.println(projectRoot);
//1.读取books.xml文件
SAXReader saxReader=new SAXReader();
Document document = saxReader.read("src/books.xml");
//2.通过Document对象获取根元素
Element rootElement = document.getRootElement();
//3.通过根元素获取book标签对象
List<Element> books = rootElement.elements("book");
List<Book> bookList=new ArrayList<>();
//4.遍历处理每个book标签转换为Book类
for(Element e:books){
//asXML()把标签对象转为标签字符串
// System.out.println(e.asXML());
Element name = e.element("name");
//System.out.println(name.asXML());
//getText()获取标签文本内容
String nameText=name.getText();
//elementText直接获取指定标签里的文本内容
String priceText=e.elementText("price");
String authorText=e.elementText("author");
String sn=e.attributeValue("id");
System.out.println(sn);
Book book=new Book();
book.setSn(sn);
book.setAuthor(authorText);
book.setName(nameText);
book.setPrice(new BigDecimal(priceText));
bookList.add(book);
}
System.out.println(bookList);
}
public static void main(String[] args) throws DocumentException {
String projectRoot = System.getProperty("user.dir");
System.out.println(projectRoot);
//1.读取books.xml文件
SAXReader saxReader=new SAXReader();
Document document = saxReader.read("src/books.xml");
//2.通过Document对象获取根元素
Element rootElement = document.getRootElement();
//3.通过根元素获取book标签对象
List<Element> books = rootElement.elements("book");
List<Book> bookList=new ArrayList<>();
//4.遍历处理每个book标签转换为Book类
for(Element e:books){
//asXML()把标签对象转为标签字符串
// System.out.println(e.asXML());
Element name = e.element("name");
//System.out.println(name.asXML());
//getText()获取标签文本内容
String nameText=name.getText();
//elementText直接获取指定标签里的文本内容
String priceText=e.elementText("price");
String authorText=e.elementText("author");
String sn=e.attributeValue("id");
System.out.println(sn);
Book book=new Book();
book.setSn(sn);
book.setAuthor(authorText);
book.setName(nameText);
book.setPrice(new BigDecimal(priceText));
bookList.add(book);
}
System.out.println(bookList);
}
}
上面@Test可以执行,main方法找不到文件的原因
这是因为在测试中,测试运行器可能会设置不同的当前工作目录(working directory)。