欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > XML DOM4J 三、XPath

XML DOM4J 三、XPath

2025/6/26 21:30:25 来源:https://blog.csdn.net/best_virtuoso/article/details/145807496  浏览:    关键词:XML DOM4J 三、XPath
1 什么是XPath

XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言。XPath基于XML的树状结构,提供在数据结构树中找寻节点的能力。起初 XPath 的提出的初衷是将其作为一个通用的、介于XPointer与XSL间的语法模型。但是 XPath 很快的被开发者采用来当作小型查询语言。

2 DOM4J对XPath的支持

在DOM4J中,Node接口中的三个方法最为常用: 

  1. List selectNodes(String xpathExpression):在当前节点中查找满足XPath表达式的所有子节点;
  2. Node selectSingleNode(String xpathExpression):在当前节点中查找满足XPath表达式的第一个子节点;
  3. String valueOf(String xpathExpression):在当前节点中查找满足XPath表达式的第一个子节点的文本内容;
Document doc = reader.read(new FileInputStream("person.xml"));Element root = doc.getRootElement();List<Element> eList = root.selectNodes("/persons");List<Element> eList1 = root.selectNodes("/persons/person/name");List<Element> eList2 = root.selectNodes("//age");List<Element> eList3 = root.selectNodes("//person/age");List<Element> eList4 = root.selectNodes("/*/*/address");List<Element> eList5 = root.selectNodes("//*");List<Element> eList6 = root.selectNodes("/persons/person[1]");List<Element> eList7 = root.selectNodes("/persons/person[last()]");List<Element> eList8 = root.selectNodes("//@id");List<Element> eList9 = root.selectNodes("//person[@id]");List<Element> eList10 = root.selectNodes("//person[@*]");List<Element> eList11 = root.selectNodes("//*[not(@*)]");

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词