XPath技术

来源:互联网 发布:网络推广赚钱 编辑:程序博客网 时间:2024/06/16 01:44

XPath技术

基本概述

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

PS:其配合DOM4J解析技术,弥补了DOM4J不能跨层取元素的缺点。需要引入jaxen-1.1-beta-6.jar包。

 

XPath原理

    XPath就好比SQL查询语句,能够对DOM树进行查询操作,并获取相应结果。

 

XPath案例

XML9.xml

<?xml version="1.0" encoding="utf-8"?><AAA><BBB id="b1">Hello World B1</BBB><CCC id="c1"/><BBB id="b2">Hello World B2</BBB><BBB>Hello World B3</BBB><DDD><BBB id="b3">Hello World B4</BBB></DDD><CCC><DDD><BBB id="b4"/><BBB id="b5"/></DDD></CCC></AAA> 

package com.pc;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.Iterator;import java.util.List;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;/** *  * @author Switch * @function DOM4j配合xpath *  */public class XML9 {public static void main(String[] args) throws Exception {// 1.得到SAXReader解析器SAXReader saxReader = new SAXReader();// 2.指定解析哪个文件Document document = saxReader.read("src/com/pc/XML9.xml");// 3.使用XPath随意读取任何一层的元素// document.selectNodes(); //返回多个元素// document.selectSingleNode(); // 返回一个元素// 取出AAA下面的所有BBB 3个// List nodeList = document.selectNodes("/AAA/BBB");// 取出所有的BBB 6个// List nodeList = document.selectNodes("//BBB");// System.out.println(nodeList.size());// 取出AAA下面的DDD下面的最后一个BBB元素的内容 Hello World B4// List nodeList = document.selectNodes("/AAA/DDD//BBB[last()]");// System.out.println(((Element)nodeList.get(0)).getTextTrim());// 取出AAA下面的CCC下面的DDD下面的所有元素 2个// List nodeList = document.selectNodes("/AAA/CCC/DDD/*");// 取出所有有三个祖先元素的BBB元素// List nodeList = document.selectNodes("/*/*/*/BBB");// System.out.println(nodeList.size());// 取出AAA下面的第一个BBB元素的内容 Hello World B1// List nodeList = document.selectNodes("/AAA/BBB[1]");// System.out.println(((Element)nodeList.get(0)).getTextTrim());// Element element = (Element) document.selectSingleNode("/AAA/BBB[1]");// System.out.println(element.getTextTrim());// 取出所有有id属性的元素的id属性 5个// List nodeList = document.selectNodes("//@id");// System.out.println(nodeList.size());// 取出第一个有id属性的元素的id属性的值 b1// System.out.println(((Attribute)nodeList.get(0)).getText());// 取出所有有id属性的CCC元素// List nodeList = document.selectNodes("//CCC[@id]");// System.out.println(nodeList.size());// "//BBB[@*]" 选择有任意属性的BBB元素// "//BBB[not(@*)]" 选择没有属性的BBB元素// "//BBB[@id='b1']" 选择含有属性id且其值为'b1'的BBB元素// 选择含有属性id且其值(在用normalize-space函数去掉前后空格后)为'b2'的BBB元素// "//BBB[normalize-space(@id)='b2']"// "//*[count(BBB)=2]" 选择含有2个BBB子元素的元素// "//*[name()='BBB']" 选择所有名称为BBB的元素(这里等价于//BBB)// "//*[starts-with(name(),'B')]" 选择所有名称以"B"起始的元素// "//*[contains(name(),'C')]" 选择所有名称包含"C"的元素// "//*[string-length(name()) = 3]" 选择名字长度为3的元素// "//*[string-length(name()) < 4]" 选择名字长度小于3的元素// 同样的将<替换成大于号则是大于// 11个// List nodeList = document.selectNodes("//*[string-length(name()) < 4]");// System.out.println(nodeList.size());// "//CCC | //BBB" 选择所有的CCC和BBB元素}}

PS:通过上述案例中的XPath语句,基本的XML查询,使用是没问题的。


0 0
原创粉丝点击