Using Xpath in Dom4j

自作多情 提交于 2020-01-12 06:29:26

问题


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!


回答1:


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.




回答2:


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.




回答3:


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!




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/1669625/using-xpath-in-dom4j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!