在Dom4j中使用Xpath [英] Using Xpath in Dom4j

查看:236
本文介绍了在Dom4j中使用Xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试访问dom4j上已解析的xml文档的任何节点时,出现以下异常:

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)

我知道解析有效,因为我可以让解析器打印出xml文档或将其保存到文件中.这是我正在使用的代码.

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.

要解析文档:

 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'");

这将打印出xml doc,我认为这意味着doc(已解析的xml doc)包含了它应该包含的内容.

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());

非常感谢您的帮助!

推荐答案

您应在您的库中添加 jaxen 库类路径.

You should add jaxen library to your class path.

编辑:实际上是原始的dom4j 发行版中包含jaxen.jar以及所有其他依赖项.

EDIT: Actually original dom4j distribution contains jaxen.jar in that as well as all other dependencies.

这篇关于在Dom4j中使用Xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆