Home  >  Article  >  Backend Development  >  XPath technology

XPath technology

黄舟
黄舟Original
2017-02-20 15:14:541453browse

XPathTechnology

##Basic Overview


XPath

is the XML path language, which is a path language used to determine XML (subset of Standard Universal Markup Language) The language of a portion of a document. XPathThe tree structure based on XML provides the ability to find nodes in the data structure tree. The original intention of XPath was to use it as a universal interface between XPointer and XSLT# Grammar model between ##. But XPath was quickly adopted by developers as a small query language.

PS

: It cooperates with DOM4J parsing technology to make up for DOM4J The disadvantage of not being able to get elements across layers. The jaxen-1.1-beta-6.jar package needs to be introduced.

XPath

Principle

XPath

is like

SQLQuery statement can query the DOM tree and obtain the corresponding results.

XPath

Case

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=&#39;b1&#39;]" 选择含有属性id且其值为&#39;b1&#39;的BBB元素

		// 选择含有属性id且其值(在用normalize-space函数去掉前后空格后)为&#39;b2&#39;的BBB元素
		// "//BBB[normalize-space(@id)=&#39;b2&#39;]"

		// "//*[count(BBB)=2]" 选择含有2个BBB子元素的元素

		// "//*[name()=&#39;BBB&#39;]" 选择所有名称为BBB的元素(这里等价于//BBB)

		// "//*[starts-with(name(),&#39;B&#39;)]" 选择所有名称以"B"起始的元素

		// "//*[contains(name(),&#39;C&#39;)]" 选择所有名称包含"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

: Through the XPath statement in the above case, the basic XML query, there is no problem in using it.

The above is the content of XPath technology. For more related content, please pay attention to the PHP Chinese website (www.php .cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:XML Programming-DOM4JNext article:XML Programming-DOM4J