Java 在 Dom4j 中使用 Xpath
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1669625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Xpath in Dom4j
提问by Daniel Hawthorne
I get the following exception when trying to access any nodes of a parsed xml document on dom4j:
尝试访问 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)
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.
我知道解析有效,因为我可以让解析器打印出 xml 文档或将其保存到文件中。这是我正在使用的代码。
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.
这将打印出 xml doc,我认为这意味着 doc(这是解析的 xml doc)包含它应该包含的内容。
System.out.println(doc.asXML());
I really appreciate your help!
我真的很感谢你的帮助!
采纳答案by Andrey Adamovich
You should add jaxenlibrary to your class path.
您应该将jaxen库添加到您的类路径中。
EDIT: Actually original dom4j distributioncontains jaxen.jar in that as well as all other dependencies.
编辑:实际上原始 dom4j发行版中包含 jaxen.jar 以及所有其他依赖项。
回答by Tendayi Mawushe
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.
java.lang.NoClassDefFoundError
当加载类以供 JVM 使用时,在类路径上找不到编译特定类时可用的依赖项时,JVM 会抛出A。
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.
你如何调用解析器代码?检查并确保lib
DOM4J 分发文件夹中的所有 DOM4J 依赖项(jaxen、jaxme-api 等)都在类路径上。
If you are invoking the parser from the command line you can use the -classpath
option:
如果您从命令行调用解析器,则可以使用以下-classpath
选项:
java -classpath C:\myjars\jar1.jar;C:\myjars\jar1.jar
If you are invoking the parser from Ant for example use the <classpath>
tag:
例如,如果您从 Ant 调用解析器,请使用以下<classpath>
标记:
<classpath>
<pathelement path="C:\myjars\jar1.jar"/>
<pathelement path="C:\myjars\jar2.jar"/>
</classpath>
Your xpath expressions are not even being evaluated so you should stoptweaking those until you have sorted out your classpath issues.
你的 xpath 表达式甚至没有被评估,所以你应该停止调整它们,直到你解决了你的类路径问题。
回答by Daniel Hawthorne
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!
因此,如果我在 jdom4 jar 之外还包含 jaxen-1.1-beta-6.jar,则 xpath 可以工作。请注意 jaxen-1.1.1.jar 不起作用。如果您有来自 jdom 的 classdef 错误,请查看他们的依赖项并确保您使用的是他们批准的 jars(对于 1.6.1 版本,现在通常是 jar 的旧版本)。希望这可以帮助任何有类似问题的人。再次感谢大家的帮助!
回答by James
If you're using mvn2, the following will work with dom4j 1.6.1:
如果您使用的是 mvn2,以下内容将适用于 dom4j 1.6.1:
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.1</version>
</dependency>
That being said, I hope they fix their pom and save everyone this trouble.
话虽如此,我希望他们修复他们的 pom 并为大家省去这个麻烦。
回答by noMad17
In the unlikely event that anyone else should encounter this issue in JBoss Fuse I'll add what solved my problem:
万一其他人在 JBoss Fuse 中遇到此问题,我将添加解决我问题的方法:
You will need to wrap both the jaxen- and the dom4j jars as OSGi-bundles.
您需要将 jaxen 和 dom4j jar 打包为 OSGi 包。
osgi:install -s wrap:mvn:jaxen/jaxen/1.1-beta-6
osgi:install -s wrap:mvn:dom4j/dom4j/1.6.1
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.
按照那个特定的顺序,正如我发现的那样。我已经包装了 dom4j jar 并在失败后简单地添加了 jaxen jar。