Search code examples
javaxmldom4j

Using Xpath in Dom4j


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!


Solution

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