Using Xpath in Dom4j
Asked Answered
T

5

23

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!

Terreverte answered 3/11, 2009 at 19:29 Comment(0)
G
19

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.

Genesia answered 3/11, 2009 at 19:42 Comment(3)
I thought the distribution included the jaxen lib as well, but I included the jar now just to see, and it did remove the error message. Unfortunately it gave me a new one. So now it complains at the end bracket of a code block using the parsed document that "it reached the end of the file while parsing." Now this occurs even on code that traversed the tree through iterators (which worked before). I would think this means that the new jaxon jar is messing with the parsing, but I can still parse and then print the file out again, so that doesn't seem to be the case.Terreverte
NoClassDefFoundException can only mean that you either don't have the needed class on your class path or there is the wrong version of that. Check you class path once again. Remove unneeded jars and add only the ones that are used.Genesia
the only jars I had originally was the dom4j-1.6.1.jar, which gave me the class def error. I added the jaxen-1.1.1.jar which got rid of the class def error and gave me the "reached end of file while parsing error." Thanks for your help and quick response Superfilin.Terreverte
K
20

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.

Kiona answered 3/12, 2010 at 23:4 Comment(0)
G
19

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.

Genesia answered 3/11, 2009 at 19:42 Comment(3)
I thought the distribution included the jaxen lib as well, but I included the jar now just to see, and it did remove the error message. Unfortunately it gave me a new one. So now it complains at the end bracket of a code block using the parsed document that "it reached the end of the file while parsing." Now this occurs even on code that traversed the tree through iterators (which worked before). I would think this means that the new jaxon jar is messing with the parsing, but I can still parse and then print the file out again, so that doesn't seem to be the case.Terreverte
NoClassDefFoundException can only mean that you either don't have the needed class on your class path or there is the wrong version of that. Check you class path once again. Remove unneeded jars and add only the ones that are used.Genesia
the only jars I had originally was the dom4j-1.6.1.jar, which gave me the class def error. I added the jaxen-1.1.1.jar which got rid of the class def error and gave me the "reached end of file while parsing error." Thanks for your help and quick response Superfilin.Terreverte
T
5

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!

Terreverte answered 7/11, 2009 at 1:13 Comment(0)
S
1

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.

Sunken answered 3/11, 2009 at 20:29 Comment(0)
C
0

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.

Caitlin answered 8/11, 2016 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.