Using Xpath in Dom4j

2019-03-23 05:46发布

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!

标签: java xml dom4j
5条回答
唯我独甜
2楼-- · 2019-03-23 05:55

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楼-- · 2019-03-23 06:03

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.

查看更多
时光不老,我们不散
4楼-- · 2019-03-23 06:12

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.

查看更多
不美不萌又怎样
5楼-- · 2019-03-23 06:16

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.

查看更多
神经病院院长
6楼-- · 2019-03-23 06:20

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!

查看更多
登录 后发表回答