问题描述
java 代码: import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class TestXML03 {public static void main(String[] args) throws DocumentException { SAXReader r=new SAXReader();Document d=r.read(TestXML03.class.getClassLoader().getResource("xml/test.xml"));Element root=d.getRootElement();List<Element> eles=root.selectNodes("book");}}XML:<?xml version="1.0" encoding="UTF-8"?><books><book id="1"> <name>java编程思想</name> <auoth>日本AV女郎</auoth> <price>312</price></book><book id="2"> <name>C++编程思想</name> <auoth>韩国AV女郎</auoth> <price>312</price></book><book id="3"> <name>PHP编程思想</name> <auoth>越南AV女郎</auoth> <price>312</price></book></books>异常:Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenExceptionat org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:164)at FirstOne.TestXML03.main(TestXML03.java:15)Caused by: java.lang.ClassNotFoundException: org.jaxen.JaxenExceptionat java.net.URLClassLoader$1.run(URLClassLoader.java:200)at java.security.AccessController.doPrivileged(Native Method)at java.net.URLClassLoader.findClass(URLClassLoader.java:188)at java.lang.ClassLoader.loadClass(ClassLoader.java:307)at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)at java.lang.ClassLoader.loadClass(ClassLoader.java:252)at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)... 4 more红色那段报错很诡异 实在没有办法啦
解决方案
由于dom4j在使用selectNodes方法是依赖jaxen的,所以需要导入这个jar,下载地址为:http://dist.codehaus.org/jaxen/distributions/jaxen-1.1.1.zip
解决方案二:
在项目中导入jaxen.jar就好了,dom4j依赖于这个jar包