2019独角兽企业重金招聘Python工程师标准>>>

关注注释和代码

xml文档:为了说明问题做了一些改动,意思不重要,看代码即可

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- <!DOCTYPE SwordLibrary SYSTEM "SwordTypeDefinition.dtd"> -->
<SwordLibrary>
<Sword sno="s1">
<SwordName>欢欣之刃</SwordName>
<Price>1000</Price>
<Attack factor="1.0">10</Attack>
</Sword>
<Sword sno="s2">
<SwordName>夜叉</SwordName>
<Price>2050</Price>
<Attack factor="2.0">30</Attack>
</Sword>
<Sword sno="s3">
<SwordName>魔棒</SwordName>
<Price type="Dollar">200</Price>
<Attack factor="1.0">0</Attack>
</Sword>
<Sword sno="s5">
<SwordName>大魔棒</SwordName>
<Price type="Dollar">559</Price>
<Attack factor="1.0">
<fire>1</fire>
<ice>2</ice>
</Attack>
</Sword>
<Sword sno="s6">
<SwordName>风杖</SwordName>
<Price type="Dollar">1880</Price>
<Attack factor="1.0">
<fire>3</fire>
<wind>2</wind>
<thunder>0</thunder>
</Attack>
</Sword><Sword sno="s9">
<SwordName>圣殿</SwordName>
<Price type="Dollar">575</Price>
<Attack factor="0.0">
<Sword sno="s7">
<SwordName>守护指环</SwordName>
<Price>250</Price>
<Attack factor="0.0">2</Attack>
</Sword>
<Sword sno="s8">
<SwordName>艺人面罩</SwordName>
<Price>325</Price>
<Attack factor="0.0">0.50</Attack>
</Sword></Attack>
</Sword></SwordLibrary>

java code:

package com.JavaLeaner;import java.io.File;
import java.util.List;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;public class XmlDemo7 {/** 对于文本节点条件的最一般例子*/@Testpublic void Test1() throws DocumentException {SAXReader reader = new SAXReader();Document doc = reader.read(new File("src/SwordLib.xml"));Element root = doc.getRootElement();Node n=root.selectSingleNode("/SwordLibrary/Sword/SwordName[text()='夜叉']");System.out.println(n.getText());/*       夜叉*/}/** 对于属性节点条件的最一般例子* 条件可以前置于上层节点*/@Testpublic void Test2() throws DocumentException{SAXReader reader = new SAXReader();Document doc = reader.read(new File("src/SwordLib.xml"));Element root = doc.getRootElement();Node n=root.selectSingleNode("/SwordLibrary/Sword[@sno='s3']/SwordName");System.out.println(n.getText());/*        魔棒*/}/** SwordLibrary的子元素中包含孙元素SwordName的子元素,即Sword*/@Testpublic void Test3() throws DocumentException{SAXReader reader = new SAXReader();Document doc = reader.read(new File("src/SwordLib.xml"));Element root = doc.getRootElement();Node n=root.selectSingleNode("/SwordLibrary/*[SwordName]");System.out.println(n.getName()); /*Sword*/}/** 本例说明了如何对目标元素的上层元素进行条件限制,即使该条件涉及父元素的其他子孙元素* 在SwordLibrary下有哪个元素含有Attack子元素,且Attack子元素下又有fire孙元素,这个元素的子元素SwordName是什么*/@Testpublic void Test4() throws DocumentException{SAXReader reader = new SAXReader();Document doc = reader.read(new File("src/SwordLib.xml"));Element root = doc.getRootElement();Node n=root.selectSingleNode("/SwordLibrary/*[Attack/fire]/SwordName");System.out.println(n.getText());
/*      大魔棒*/}/** 意在说明如何对名称未定的元素设置文本限制条件* 本例限制条件:Attack元素下任意一个子元素文本值等于3*/@Testpublic void Test5() throws DocumentException{SAXReader reader = new SAXReader();Document doc = reader.read(new File("src/SwordLib.xml"));Element root = doc.getRootElement();Node n=root.selectSingleNode("/SwordLibrary/*[Attack/*[text()='3']]/SwordName");System.out.println(n.getText()); /*       风杖*/}/** 绝对路径和相对路径(递归向下)*/@Testpublic void Test6() throws DocumentException{SAXReader reader = new SAXReader();Document doc = reader.read(new File("src/SwordLib.xml"));Element root = doc.getRootElement();List list1=root.selectNodes("/SwordLibrary/Sword");System.out.println(list1.size()); List list2=root.selectNodes("/Sword");System.out.println(list2.size()); List list3=root.selectNodes("//Sword");System.out.println(list3.size()); /*       608
*/}/** 递归向下也可以用在XPath路径中间,代表中间任意多个元素*/@Testpublic void Test7() throws DocumentException{SAXReader reader = new SAXReader();Document doc = reader.read(new File("src/SwordLib.xml"));Element root = doc.getRootElement();List<Node> ns=root.selectNodes("/SwordLibrary//SwordName");for(int i=0;i<ns.size();i++)System.out.println(ns.get(i).getText()); /*      欢欣之刃夜叉魔棒大魔棒风杖圣殿守护指环艺人面罩*/}/** 属性的条件* 条件可以嵌套,即条件中的元素、属性、或者其他函数值也可以添加条件.[[]]*/@Testpublic void Test8() throws DocumentException{SAXReader reader = new SAXReader();Document doc = reader.read(new File("src/SwordLib.xml"));Element root = doc.getRootElement();List<Node> ns=root.selectNodes("//Sword[Attack[@factor='1.0']]/SwordName");for(int i=0;i<ns.size();i++)System.out.println(ns.get(i).getText()); /*        欢欣之刃魔棒大魔棒风杖*/}/** 条件设置[]的可以凑在一起,分别描述不同的子元素条件或者text()等。[][]*/@Testpublic void Test9() throws DocumentException{SAXReader reader = new SAXReader();Document doc = reader.read(new File("src/SwordLib.xml"));Element root = doc.getRootElement();List<Node> ns=root.selectNodes("//Sword[Price>0 and Price<1000][Attack[@factor='0.0']]/SwordName");for(int i=0;i<ns.size();i++)System.out.println(ns.get(i).getText()); /*      圣殿守护指环艺人面罩*/}/** []不仅可以设置条件,还可以设置下标,注意从1开始,不是0* last()代表最后的*/@Testpublic void Test10() throws DocumentException{SAXReader reader = new SAXReader();Document doc = reader.read(new File("src/SwordLib.xml"));Element root = doc.getRootElement();Node n=root.selectSingleNode("//Sword[last()]/Attack/Sword[2]/SwordName");System.out.println(n.getText()); /*     艺人面罩*/}/** Sword[1]第一个就是第一个Sword,而不能理解为满足下述条件的第一个*/@Testpublic void Test11() throws DocumentException{SAXReader reader = new SAXReader();Document doc = reader.read(new File("src/SwordLib.xml"));Element root = doc.getRootElement();List<Node> ns=root.selectNodes("//Sword[1]/Attack/Sword[2]/SwordName");for(int i=0;i<ns.size();i++)System.out.println(ns.get(i).getText());/*     无内容*/}/** 从上下两个例子可以看出,下标使用比较特别。* 第二个变成了从两类相对路径上的两个第二个Sword*/@Testpublic void Test12() throws DocumentException{SAXReader reader = new SAXReader();Document doc = reader.read(new File("src/SwordLib.xml"));Element root = doc.getRootElement();List<Node> ns=root.selectNodes("//Sword[2]/SwordName");for(int i=0;i<ns.size();i++)System.out.println(ns.get(i).getText()); /*       夜叉艺人面罩*/}/** ../代表从父节点开始* 注意:父节点是..    不是../* ./是当前路径*/@Testpublic void Test13() throws DocumentException{SAXReader reader = new SAXReader();Document doc = reader.read(new File("src/SwordLib.xml"));Element root = doc.getRootElement();Element ename=(Element)root.selectSingleNode("//Sword[3]/SwordName");System.out.println(ename.getName()+":"+ename.getText()); Element eprice=(Element)ename.selectSingleNode("../Price");System.out.println(eprice.getName()+":"+eprice.getText()); Element esword=(Element)ename.selectSingleNode("..");System.out.println(esword.getName()); Element eAttack=(Element)esword.selectSingleNode("./Attack");//"/Attack"不可以System.out.println(eAttack.getName()+":"+eAttack.getText()); /*      SwordName:魔棒Price:200SwordAttack:0*/}/** 下表需要出现在比较条件中时,用position()*/@Testpublic void Test14() throws DocumentException{SAXReader reader = new SAXReader();Document doc = reader.read(new File("src/SwordLib.xml"));Element root = doc.getRootElement();List<Node> ns=root.selectNodes("//Sword[position()<=2]/SwordName");for(int i=0;i<ns.size();i++)System.out.println(ns.get(i).getText()); /*       欢欣之刃夜叉守护指环艺人面罩*/}/** 字符串处理函数,注意标号都是从1开始*/@Testpublic void Test15() throws DocumentException{SAXReader reader = new SAXReader();Document doc = reader.read(new File("src/SwordLib.xml"));Element root = doc.getRootElement();System.out.println("1:"); List<Node> ns=root.selectNodes("//Sword/SwordName[contains(text(),'魔')]");for(int i=0;i<ns.size();i++)System.out.println(ns.get(i).getText()); System.out.println("2:"); List<Node> ns2=root.selectNodes("//Sword[contains(SwordName,'魔')]/SwordName");for(int i=0;i<ns2.size();i++)System.out.println(ns2.get(i).getText()); System.out.println("3:"); List<Node> ns3=root.selectNodes("//Sword/SwordName[starts-with(text(),'魔') and ends-with(text(),'棒')]");for(int i=0;i<ns3.size();i++)System.out.println(ns3.get(i).getText()); System.out.println("4:"); //substring从1开始不是从0开始,一定注意!sno='s2'List<Element> ns4=root.selectNodes("//Sword[substring(@sno,2,1)='2']/SwordName");for(int i=0;i<ns4.size();i++)System.out.println(ns4.get(i).getText()); /*          1:魔棒大魔棒2:魔棒大魔棒3:魔棒4:夜叉      */}}

转载于:https://my.oschina.net/happyBKs/blog/306983

Java学习之Xml系列七:Dom4j——专题2:基于Xpath的若干复杂例子相关推荐

  1. Java学习-11 XML与JSON

    Java学习-11 XML与JSON 1.XML 1.1.简介 可扩展标记语言(eXtensible Markup Language). 特性:1. xml具有平台无关性, 是一门独立的标记语言.2. ...

  2. Java基础复习笔记系列 七 IO操作

    Java基础复习笔记系列之 IO操作 我们说的出入,都是站在程序的角度来说的.FileInputStream是读入数据.?????? 1.流是什么东西? 这章的理解的关键是:形象思维.一个管道插入了一 ...

  3. java递归遍历xml所有元素_Java学习之Xml系列二:xml按条件查询、xml递归遍历所有元素和属性...

    2019独角兽企业重金招聘Python工程师标准>>> xml中加入了几条,为了方便查询时作为示例. 话不多说见代码注释: DTD文件:SwordTypeDefinition.dtd ...

  4. java 递归遍历对象所有属性_Java学习之Xml系列二:xml按条件查询、xml递归遍历所有元素和属性...

    xml中加入了几条,为了方便查询时作为示例. 话不多说见代码注释: DTD文件:SwordTypeDefinition.dtd XML文件:SwordLib.xml SwordLibrary SYST ...

  5. Java学习打卡第七天——[再谈Collection之Set,TreeSet,泛型Generic的简介和使用]

    说明:为了文章的简洁性和方便阅读在以后的打卡过程中,我会给出核心代码 Javaee之[Collection之Set] Javaee之[Collection之TreeSet] Javaee之[Colle ...

  6. 我的Java学习之路(七)-- 模拟考试系统

    模拟考试系统 一.功能描述 二.实现代码 1. 定义考题类 2. 定义单选题类,继承考题类 3. 定义多选题类,继承考题类 4. 定义测试类 四.演示效果图 一.功能描述 定义考题类(Question ...

  7. 基于v$lock.block及request及dba_waiters或dba_blockers学习lock锁系列七

    结论 1,v$lock.block=1表明为持锁会话,而block=0,表示为被阻塞会话(当然前提是至少有2个会话,如果仅一个会话,block=0),也就是说至少要2个会话才有意义比较block的值 ...

  8. java分布式架构_Java学习五分钟系列:从单体架构转向分布式架构的难点

    Java学习五分钟系列,目的是为让大家在短时间内搞清楚一项技术的概念.优缺点和适用场景,想要深入的了解,还需要投入更多的时间. 分布式架构和单体应用架构相比,可以充分利用多机器的性能优势,提高了系统的 ...

  9. vector 插入_Java学习五分钟系列:对比Vector、ArrayList、LinkedList

    Java学习五分钟系列,目的是为让大家在短时间内搞清楚一项技术的概念.优缺点和适用场景,想要深入的了解,还需要投入更多的时间. Java的集合类,值得我们深入的学习,建议大家有时间的话,可以阅读一下源 ...

  10. Java入门-Java学习路线课程面试篇:取商 / 和取余(模) % 符号的使用

    本博客地址 | GitHub | 更多资源免费下载 取商 / 和取余(模) % 符号的使用 文章目录 1. / % 最容易出错的演示案例 2. 运行结果: 1. / % 最容易出错的演示案例 pack ...

最新文章

  1. boot spring 打印请求参数_SpringBoot打印请求参数与响应参数
  2. SAS 2.0:中端理想“零距离”?
  3. java中的IO详解(下)
  4. 单元测试(UT)、功能测试(FT)(转)
  5. MongoDB与MySQL效率对比
  6. 代替紧急通知_人员紧急替代程序与替代方案的通知
  7. noip2018 pre——Dp
  8. MySQL 源码下载教程
  9. 《应用商务统计分析》第五章 定序回归
  10. 老MacBook升级内存记
  11. 新托业模拟考试感言—了解一下参加过托业考试前辈们的经验03
  12. 四六级分数根据比例给分
  13. 记录office安装一半重启后无法继续安装
  14. Java基础知识(四) 基本类型与运算
  15. C语言中delay的用法
  16. 美国公司暂停镰状细胞基因疗法临床试验:两受试者发展为癌症
  17. 通向互联网未来的七个路标
  18. 金数据统计表自动填写
  19. Functionlan通过星际文件系统免费使用云应用程序
  20. 小册上新 | 如何使用掘金社区

热门文章

  1. 计算机基础与程序设计(一)
  2. 牛刀小试:安装proxmox VE
  3. Qt Creator - UI Designer可视化设计窗体
  4. LTE的A1事件A2事件A3事件A4事件指的都是什么?
  5. QT 禁止 LNK:4099警告
  6. 没有收款通道,怕PayPal被封,怕条纹被禁怎么办
  7. 【恒指早盘分析】9.9恒指早盘分析及最新资讯
  8. 【恒指早盘分析】9.10恒指早盘分析及最新资讯
  9. 总结oninput、onchange与onpropertychange事件的用法和区别,onchange
  10. 【Java每日一题】——第四十三题:编程用多态实现打印机.。分为黑白打印机和彩色打印机,不同类型的打印机打印效果不同。(2023.10.30)