奇怪的错误。。
import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader; import java.util.List; public class XpathTester { public void xpath(String xpExp) { String file = "C:/My Work Material/idea/xml-work/src/plan.xml"; SAXReader reader = new SAXReader(); try { Document doc = reader.read(file); List<Node> nodes = doc.selectNodes(xpExp); for (Node node : nodes) { Element emp = (Element) node; System.out.println(emp.attributeValue("id")); System.out.println(emp.elementText("course-name")); System.out.println(emp.elementText("course-hour")); System.out.println(emp.elementText("exam-form")); System.out.println("= = = = = = = = = = = = = = = ="); } } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { XpathTester xt = new XpathTester(); // xt.xpath("//course"); xt.xpath("/course[course-hour<50]"); } }
<?xml version="1.0" encoding="utf-8"?> <teaching-plan xmlns="http://www.w3school.com.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="plan.xsd"> <course id="001"> <course-name>大学英语</course-name> <course-hour>36</course-hour> <exam-form>考试</exam-form> </course> <course id="002"> <course-name>高等数学</course-name> <course-hour>70</course-hour> <exam-form>考试</exam-form> </course> <course id="003"> <course-name>计算机应用基础</course-name> <course-hour>108</course-hour> <exam-form>上机考试</exam-form> </course> <course id="004"> <course-name>体育课</course-name> <course-hour>52</course-hour> <exam-form>考试</exam-form> </course> </teaching-plan>
C:\Jdk\jdk8\bin\java.exe "-javaagent:E:\Program Files\JetBrains\IntelliJ IDEA 2019.2.3\lib\idea_rt.jar=2610:E:\Program Files\JetBrains\IntelliJ IDEA 2019.2.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Jdk\jdk8\jre\lib\charsets.jar;C:\Jdk\jdk8\jre\lib\deploy.jar;C:\Jdk\jdk8\jre\lib\ext\access-bridge-64.jar;C:\Jdk\jdk8\jre\lib\ext\cldrdata.jar;C:\Jdk\jdk8\jre\lib\ext\dnsns.jar;C:\Jdk\jdk8\jre\lib\ext\jaccess.jar;C:\Jdk\jdk8\jre\lib\ext\jfxrt.jar;C:\Jdk\jdk8\jre\lib\ext\localedata.jar;C:\Jdk\jdk8\jre\lib\ext\nashorn.jar;C:\Jdk\jdk8\jre\lib\ext\sunec.jar;C:\Jdk\jdk8\jre\lib\ext\sunjce_provider.jar;C:\Jdk\jdk8\jre\lib\ext\sunmscapi.jar;C:\Jdk\jdk8\jre\lib\ext\sunpkcs11.jar;C:\Jdk\jdk8\jre\lib\ext\zipfs.jar;C:\Jdk\jdk8\jre\lib\javaws.jar;C:\Jdk\jdk8\jre\lib\jce.jar;C:\Jdk\jdk8\jre\lib\jfr.jar;C:\Jdk\jdk8\jre\lib\jfxswt.jar;C:\Jdk\jdk8\jre\lib\jsse.jar;C:\Jdk\jdk8\jre\lib\management-agent.jar;C:\Jdk\jdk8\jre\lib\plugin.jar;C:\Jdk\jdk8\jre\lib\resources.jar;C:\Jdk\jdk8\jre\lib\rt.jar;C:\My Work Material\idea\xml-work\out\production\xml-work;C:\My Work Material\idea\xml-work\lib\dom4j-2.1.1.jar" XpathTester
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException
at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:222)
at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:202)
at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:159)
at XpathTester.xpath(XpathTester.java:14)
at XpathTester.main(XpathTester.java:31)
Caused by: java.lang.ClassNotFoundException: org.jaxen.JaxenException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 5 more
Process finished with exit code 1
正在回答
同学你好,代码编写的不错,根据报错信息,“Caused by: java.lang.ClassNotFoundException: org.jaxen.JaxenException”没有找到JaxenException类,应该是同学忘记导入jaxen的jar包啦,建议同学检查是否导入jar包。
如:
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
同学你好,这里 XML文件引入xsd文件是可以被解析的,但是在同学的代码中xml文件头中,不应该添加xmlns="http://www.w3school.com.cn" 这部分代码,因为xml文件中不需要设置默认的命名空间。我们需要在文档中声明指定文档的位置。
修改后的代码如下:
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星