一个没有引入Shema约束的XML文档
<?xml version="1.0" encoding="UTF-8"?>
<书架>
<书>
<书名>JavaWeb开发大全</书名>
<作者>班长</作者>
<售价>99.8</售价>
<简介>不错</简介>
</书>
<书>
<书名>葵花宝典</书名>
<作者>岳不群</作者>
<售价>19.8</售价>
<简介>欲练此功...</简介>
</书>
</书架>
直接使用如下代码利用XPath解析没有问题
public class Dom4jTest {
public static void main(String[] args) throws Exception {
run7();
}
private static void run7() throws Exception {
String uri = "src/book.xml";
//获取Document对象
Document doc = getDocument(uri);
//获得此节点下的节点集合
List<Node> list = doc.selectNodes("/书架/书/作者");
//遍历list,输出所有节点的文本
list.forEach(tmp -