Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Xpath in Dom4j

Tags:

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!

like image 361
Daniel Hawthorne Avatar asked Nov 03 '09 19:11

Daniel Hawthorne


2 Answers

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.

like image 165
James Avatar answered Sep 24 '22 01:09

James


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.

like image 41
Andrey Adamovich Avatar answered Sep 22 '22 01:09

Andrey Adamovich