获取节点的代码出现了问题!老师求助

获取节点的代码出现了问题!老师求助

# 具体遇到的问题

老师 这里获取节点的代码好像出现了问题!List <Node> nodes = document.selectNodes("/root/category");这条代码报错了!

# 报错信息的截图
http://img1.sycdn.imooc.com//climg/5fb247d5094352ea19620907.jpg

# 相关课程内容截图

http://img1.sycdn.imooc.com//climg/5fb247ed09e6b6ca06320848.jpg

http://img1.sycdn.imooc.com//climg/5fb247ed09167bce11450522.jpg


# 尝试过的解决思路和结果

# 粘贴全部相关代码,切记添加代码注释(请勿截图)

在这里输入代码,可通

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 + "]";
}

}


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

同学你好,老师测试代码是可以正常运行的,如下图所示:则建议同学换一个项目试一下。

http://img1.sycdn.imooc.com//climg/5fb255da09e00da108540142.jpg

  • 金鱼眼 提问者 #1
    换了一个项目还是不行老师!但我尝试运行前面的课程项目mgallery里的XmlDataSource却可以运行!怎么办
    2020-11-17 15:08:59
  • 金鱼眼 提问者 #2
    问题已解决!原来是漏了一个jaxen.jar包
    2020-11-17 15:31:47
金鱼眼 提问者 2020-11-17 15:10:36

http://img1.sycdn.imooc.com//climg/5fb3775b090e325415971041.jpg

运行后就弹出这个

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师