XPath technology
XPathTechnology
##Basic Overview
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
PrincipleXPath
is likeSQLQuery statement can query the DOM tree and obtain the corresponding results.
XPath
CaseXML9.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元素 } }
: 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)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The speed of mobile XML to PDF depends on the following factors: the complexity of XML structure. Mobile hardware configuration conversion method (library, algorithm) code quality optimization methods (select efficient libraries, optimize algorithms, cache data, and utilize multi-threading). Overall, there is no absolute answer and it needs to be optimized according to the specific situation.

It is impossible to complete XML to PDF conversion directly on your phone with a single application. It is necessary to use cloud services, which can be achieved through two steps: 1. Convert XML to PDF in the cloud, 2. Access or download the converted PDF file on the mobile phone.

It is not easy to convert XML to PDF directly on your phone, but it can be achieved with the help of cloud services. It is recommended to use a lightweight mobile app to upload XML files and receive generated PDFs, and convert them with cloud APIs. Cloud APIs use serverless computing services, and choosing the right platform is crucial. Complexity, error handling, security, and optimization strategies need to be considered when handling XML parsing and PDF generation. The entire process requires the front-end app and the back-end API to work together, and it requires some understanding of a variety of technologies.

XML formatting tools can type code according to rules to improve readability and understanding. When selecting a tool, pay attention to customization capabilities, handling of special circumstances, performance and ease of use. Commonly used tool types include online tools, IDE plug-ins, and command-line tools.

To open a web.xml file, you can use the following methods: Use a text editor (such as Notepad or TextEdit) to edit commands using an integrated development environment (such as Eclipse or NetBeans) (Windows: notepad web.xml; Mac/Linux: open -a TextEdit web.xml)

There are three ways to convert XML to Word: use Microsoft Word, use an XML converter, or use a programming language.

An application that converts XML directly to PDF cannot be found because they are two fundamentally different formats. XML is used to store data, while PDF is used to display documents. To complete the transformation, you can use programming languages and libraries such as Python and ReportLab to parse XML data and generate PDF documents.

XML can be converted to images by using an XSLT converter or image library. XSLT Converter: Use an XSLT processor and stylesheet to convert XML to images. Image Library: Use libraries such as PIL or ImageMagick to create images from XML data, such as drawing shapes and text.
