search

XPath technology

Feb 20, 2017 pm 03:14 PM

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
RSS in XML: Unveiling the Core of Content SyndicationRSS in XML: Unveiling the Core of Content SyndicationApr 22, 2025 am 12:08 AM

The implementation of RSS in XML is to organize content through a structured XML format. 1) RSS uses XML as the data exchange format, including elements such as channel information and project list. 2) When generating RSS files, content must be organized according to specifications and published to the server for subscription. 3) RSS files can be subscribed through a reader or plug-in to automatically update the content.

Beyond the Basics: Advanced RSS Document FeaturesBeyond the Basics: Advanced RSS Document FeaturesApr 21, 2025 am 12:03 AM

Advanced features of RSS include content namespaces, extension modules, and conditional subscriptions. 1) Content namespace extends RSS functionality, 2) Extended modules such as DublinCore or iTunes to add metadata, 3) Conditional subscription filters entries based on specific conditions. These functions are implemented by adding XML elements and attributes to improve information acquisition efficiency.

The XML Backbone: How RSS Feeds are StructuredThe XML Backbone: How RSS Feeds are StructuredApr 20, 2025 am 12:02 AM

RSSfeedsuseXMLtostructurecontentupdates.1)XMLprovidesahierarchicalstructurefordata.2)Theelementdefinesthefeed'sidentityandcontainselements.3)elementsrepresentindividualcontentpieces.4)RSSisextensible,allowingcustomelements.5)Bestpracticesincludeusing

RSS & XML: Understanding the Dynamic Duo of Web ContentRSS & XML: Understanding the Dynamic Duo of Web ContentApr 19, 2025 am 12:03 AM

RSS and XML are tools for web content management. RSS is used to publish and subscribe to content, and XML is used to store and transfer data. They work with content publishing, subscriptions, and update push. Examples of usage include RSS publishing blog posts and XML storing book information.

RSS Documents: The Foundation of Web SyndicationRSS Documents: The Foundation of Web SyndicationApr 18, 2025 am 12:04 AM

RSS documents are XML-based structured files used to publish and subscribe to frequently updated content. Its main functions include: 1) automated content updates, 2) content aggregation, and 3) improving browsing efficiency. Through RSSfeed, users can subscribe and get the latest information from different sources in a timely manner.

Decoding RSS: The XML Structure of Content FeedsDecoding RSS: The XML Structure of Content FeedsApr 17, 2025 am 12:09 AM

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

How to Parse and Utilize XML-Based RSS FeedsHow to Parse and Utilize XML-Based RSS FeedsApr 16, 2025 am 12:05 AM

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor