在 Dom4j 中使用 Xpath

发布于 2024-08-10 01:57:16 字数 1225 浏览 2 评论 0 原文

当我尝试访问 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

金橙橙 2024-08-17 01:57:16

如果您使用 mvn2,以下内容将适用于 dom4j 1.6.1:

<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.1</version>
</dependency>

话虽这么说,我希望他们修复他们的 pom 并为每个人省去这个麻烦。

If you're using mvn2, the following will work with 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.

你的他你的她 2024-08-17 01:57:16

您应该将 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.

青春如此纠结 2024-08-17 01:57:16

因此,如果除了 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!

简单 2024-08-17 01:57:16

当加载该类以供 JVM 使用时,在类路径中找不到编译特定类时可用的依赖项时,JVM 会抛出 java.lang.NoClassDefFoundError 错误。

您如何调用解析器代码?检查并确保 DOM4J 发行版的 lib 文件夹中的所有 DOM4J 依赖项(jaxen、jaxme-api 等)都位于类路径中。

如果您从命令行调用解析器,则可以使用 -classpath 选项:

java -classpath C:\myjars\jar1.jar;C:\myjars\jar1.jar

如果您从 Ant 调用解析器,请使用 标记:

<classpath>  
    <pathelement path="C:\myjars\jar1.jar"/>  
    <pathelement path="C:\myjars\jar2.jar"/>
</classpath> 

你的 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:

java -classpath C:\myjars\jar1.jar;C:\myjars\jar1.jar

If you are invoking the parser from Ant for example use the <classpath> tag:

<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.

任性一次 2024-08-17 01:57:16

万一其他人在 JBoss Fuse 中遇到这个问题,我将添加解决我的问题的方法:

您需要将 jaxen- 和 dom4j jar 包装为 OSGi-bundles。

osgi:install -s wrap:mvn:jaxen/jaxen/1.1-beta-6
osgi:install -s wrap:mvn:dom4j/dom4j/1.6.1

按照这个特定的顺序,我费了九牛二虎之力才发现。
我已经包装了 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文