获取节点的代码出现了问题!老师求助
# 具体遇到的问题
老师 这里获取节点的代码好像出现了问题!List <Node> nodes = document.selectNodes("/root/category");这条代码报错了!
# 报错信息的截图
# 相关课程内容截图
# 尝试过的解决思路和结果
# 粘贴全部相关代码,切记添加代码注释(请勿截图)
在这里输入代码,可通
package com.imooc.library.utils;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import com.imooc.library.entity.Book;
import com.imooc.library.entity.Category;
import com.imooc.library.entity.User;
/**
* 数据源类 解析XML
*
* @author zjczbq
*
*/
public class XmlDataSource {
private static List<User> userData = new ArrayList();
private static List<Category> categoryData = new ArrayList();
private static List<Book> bookData = new ArrayList();
private static String userDataFile;
private static String categoryDataFile;
private static String bookDataFile; //保存图书的文件地址
static {
// 下面代码意思是:在程序运行以后去得到类路径目录下的book.xml文件的路径地址
// bookDataFile = XmlDataSource.class.getResource("/book.xml").getPath();
// System.out.println(bookDataFile);
categoryDataFile = XmlDataSource.class.getResource("/category.xml").getPath();
System.out.println(categoryDataFile);
reloadUser();
reloadCategory();
// reloadBooks();
}
private static void reloadUser() {
}
private static void reloadCategory() {
URLDecoder decoder = new URLDecoder();
try {
categoryDataFile = decoder.decode(categoryDataFile, "UTF-8");
System.out.println(categoryDataFile);
SAXReader reader = new SAXReader();
Document document = reader.read("file:///"+categoryDataFile);
List <Node> nodes = document.selectNodes("/root/category");
categoryData.clear();
for(Node node : nodes) {
Element element = (Element)node;
Category category = new Category();
category.setCategoryId(element.elementText("categoryId"));
category.setCategoryName(element.elementText("categoryName"));
categoryData.add(category);
System.out.println(category);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void reloadBooks() {
URLDecoder decoder = new URLDecoder();
try {
bookDataFile = decoder.decode(bookDataFile, "UTF-8");
System.out.println(bookDataFile);
SAXReader reader = new SAXReader();
// 1.获取Document文档对象
// 如果文件目录含有中文!可以在加载文件时,前边加上 file:/// 。
Document document = reader.read("file:///"+bookDataFile);
//2.Xpath得到XML节点集合
List<Node> nodes = document.selectNodes("/root/book");
// Node是所有类型XML节点的统称,Element则通常指的是XML中的标签以及标签中所指代的信息
bookData.clear();
for(Node node : nodes) {
Element element = (Element)node;
Book book = new Book();
book.setBookId(Integer.parseInt(element.elementText("bookId")));
book.setBookName(element.elementText("bookName"));
book.setBookCategory(element.elementText("bookCategory"));
book.setBookPrice(Integer.parseInt(element.elementText("bookPrice")));
book.setBookCover(element.elementText("preview"));
book.setBookDescription(element.elementText("description"));
bookData.add(book);
//System.out.println(book);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new XmlDataSource();
}
}过选择【代码语言】突出显示
<?xml version="1.0" encoding="UTF-8"?>
<!-- 图书数据 -->
<root>
<book id="1"> <!-- 图书序号 -->
<bookId>book0001</bookId>
<bookName>Java基础</bookName>
<bookCategory>ca0001</bookCategory>
<bookPrice>29</bookPrice>
<preview>/image/g1.jpg</preview>
<description></description>
</book>
</root><?xml version="1.0" encoding="UTF-8"?>
<root>
<category id="1"> <!-- 图书分类序号 -->
<categoryId>ca0001</categoryId>
<categoryName>计算机</categoryName>
</category>
</root>package com.imooc.library.entity;
public class Book {
private Integer bookId; //图书ID
private String bookName; //图书名
private String bookCategory; //图书分类
private Integer bookPrice; //图书价格
private String bookCover; //图书封面地址
private String bookDescription; //图书备注
public Book() {
}
public Book(Integer bookId, String bookName, String bookCategory, Integer bookPrice, String bookCover,
String bookDescription) {
super();
this.bookId = bookId;
this.bookName = bookName;
this.bookCategory = bookCategory;
this.bookPrice = bookPrice;
this.bookCover = bookCover;
this.bookDescription = bookDescription;
}
public Integer getBookId() {
return bookId;
}
public void setBookId(Integer bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookCategory() {
return bookCategory;
}
public void setBookCategory(String bookCategory) {
this.bookCategory = bookCategory;
}
public Integer getBookPrice() {
return bookPrice;
}
public void setBookPrice(Integer bookPrice) {
this.bookPrice = bookPrice;
}
public String getBookCover() {
return bookCover;
}
public void setBookCover(String bookCover) {
this.bookCover = bookCover;
}
public String getBookDescription() {
return bookDescription;
}
public void setBookDescription(String bookDescription) {
this.bookDescription = bookDescription;
}
@Override
public String toString() {
return "Book [bookId=" + bookId + ", bookName=" + bookName + ", bookCategory=" + bookCategory + ", bookPrice="
+ bookPrice + ", bookCover=" + bookCover + ", bookDescription=" + bookDescription + "]";
}
}package com.imooc.library.entity;
/**
* JavaBeen
*
* @author zjczbq
*
*/
public class Category {
private String categoryId; //图书分类id
private String categoryName; //分类名称
public Category() {
}
public Category(String categoryId, String categoryName) {
super();
this.categoryId = categoryId;
this.categoryName = categoryName;
}
public String getCategoryId() {
return categoryId;
}
public void setCategoryId(String categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
@Override
public String toString() {
return "Category [categoryId=" + categoryId + ", categoryName=" + categoryName + "]";
}
}
16
收起
正在回答
2回答
同学你好,老师测试代码是可以正常运行的,如下图所示:则建议同学换一个项目试一下。
java工程师2020版
- 参与学习 人
- 提交作业 9400 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星