在 Dom4j 中使用 Xpath
当我尝试访问 dom4j 上解析的 xml 文档的任何节点时,出现以下异常:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException
at 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 xmlparser.LevelsExtractor.findI(LevelsExtractor.java:73)
at xmlparser.Main.main(Main.java:33)
我知道解析有效,因为我可以让解析器打印出 xml 文档或将其保存到文件中。这是我正在使用的代码。
解析文档:
public class Parser {
public Document parseWithSAX(File aFile) throws DocumentException {
SAXReader xmlReader = new SAXReader();
Document doc = xmlReader.read(aFile);
return doc;
}
为了尝试获取节点,我尝试了以下几行,所有这些都会产生相同的错误:
List list = doc.selectNodes("");
QName qn = new QName("////Token/text()='Introduction'");
Element el = doc.selectSingleNode("////Token/text()='Introduction'");
Node node = doc.selectSingleNode( "/DOCUMENT/PAGE/TEXT/TOKEN/text()= 'Introduction'");
这将打印出 xml 文档,我认为这意味着 doc (这是解析的 xml 文档)包含什么它应该。
System.out.println(doc.asXML());
我真的很感谢你的帮助!
I get the following exception when trying to access any nodes of a parsed xml document on dom4j:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException
at 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 xmlparser.LevelsExtractor.findI(LevelsExtractor.java:73)
at xmlparser.Main.main(Main.java:33)
I know that the parsing works, because I can have the parser print out the xml document or save it to file. Here is the code I'm using.
To parse the document:
public class Parser {
public Document parseWithSAX(File aFile) throws DocumentException {
SAXReader xmlReader = new SAXReader();
Document doc = xmlReader.read(aFile);
return doc;
}
To try to get a node I've tried the following lines, all of which produce the same error:
List list = doc.selectNodes("");
QName qn = new QName("////Token/text()='Introduction'");
Element el = doc.selectSingleNode("////Token/text()='Introduction'");
Node node = doc.selectSingleNode( "/DOCUMENT/PAGE/TEXT/TOKEN/text()= 'Introduction'");
This will print out the xml doc which I assume means that doc (which is the parsed xml doc) contains what it should.
System.out.println(doc.asXML());
I really appreciate your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您使用 mvn2,以下内容将适用于 dom4j 1.6.1:
话虽这么说,我希望他们修复他们的 pom 并为每个人省去这个麻烦。
If you're using mvn2, the following will work with dom4j 1.6.1:
That being said, I hope they fix their pom and save everyone this trouble.
您应该将 jaxen 库添加到您的类路径中。
编辑:实际上是原始的dom4j 发行版 包含 jaxen.jar 以及所有其他依赖项。
You should add jaxen library to your class path.
EDIT: Actually original dom4j distribution contains jaxen.jar in that as well as all other dependencies.
因此,如果除了 jdom4 jar 之外我还包含 jaxen-1.1-beta-6.jar,xpath 就可以工作。请注意,jaxen-1.1.1.jar 不起作用。如果您有来自 jdom 的 classdef 错误,请查看它们的依赖项并确保您使用的是它们批准的 jar(对于 1.6.1 版本来说,现在通常是该 jar 的旧版本)。希望这对遇到类似问题的人有所帮助。再次感谢大家的帮助!
So xpath works if I include jaxen-1.1-beta-6.jar in addition to the jdom4 jar. Note the jaxen-1.1.1.jar does not work. If you have a classdef error from jdom look at their dependencies and make sure you are using their approved jars, (which for the 1.6.1 version is now often an older release of the jar). Hope this helps anyone with a similar problem. Thanks again for everyone's help!
当加载该类以供 JVM 使用时,在类路径中找不到编译特定类时可用的依赖项时,JVM 会抛出 java.lang.NoClassDefFoundError 错误。
您如何调用解析器代码?检查并确保 DOM4J 发行版的
lib
文件夹中的所有 DOM4J 依赖项(jaxen、jaxme-api 等)都位于类路径中。如果您从命令行调用解析器,则可以使用
-classpath
选项:如果您从 Ant 调用解析器,请使用
标记:你的 xpath 表达式甚至没有被评估,所以你应该停止调整它们,直到你解决了你的类路径问题。
A
java.lang.NoClassDefFoundError
is thrown by the JVM when a dependency that was available at the time a particular class was compiled cannot be found on the classpath when the class is loaded for use by the JVM.How are you invoking the parser code? Check and make sure that all the DOM4J dependencies in the
lib
folder of the DOM4J distribution (jaxen, jaxme-api etc) are on the classpath.If you are invoking the parser from the command line you can use the
-classpath
option:If you are invoking the parser from Ant for example use the
<classpath>
tag:Your xpath expressions are not even being evaluated so you should stoptweaking those until you have sorted out your classpath issues.
万一其他人在 JBoss Fuse 中遇到这个问题,我将添加解决我的问题的方法:
您需要将 jaxen- 和 dom4j jar 包装为 OSGi-bundles。
按照这个特定的顺序,我费了九牛二虎之力才发现。
我已经包装了 dom4j jar,并在事实不成功后简单地添加了 jaxen jar。
In the unlikely event that anyone else should encounter this issue in JBoss Fuse I'll add what solved my problem:
You will need to wrap both the jaxen- and the dom4j jars as OSGi-bundles.
In that particular order, as I found out the hard way.
I had already wrapped the dom4j jar and simply adding the jaxen jar after the fact was unsuccessful.