tags:

views:

883

answers:

3

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  A: 

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.

Superfilin
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.
Daniel Hawthorne
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.
Superfilin
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.
Daniel Hawthorne
A: 

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.

Tendayi Mawushe
+1  A: 

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!

Daniel Hawthorne