PHP中的XML應(yīng)用(二)
Jun 21, 2016 am 09:13 AMxml
如何對文檔進(jìn)行解析?
在完成所有的準(zhǔn)備工作后,現(xiàn)在腳本終于可以解析XML文檔:
Xml_parse_from_file(),一個自定義的函數(shù),打開參數(shù)中指定的文件,并以4kb的大小進(jìn)行解析
xml_parse(),和xml_parse_from_file()一樣,當(dāng)發(fā)生錯誤時(shí),即XML文檔的格式不完全時(shí),將會返回false。
我們可以使用xml_get_error_code()函數(shù)來得到最后一個錯誤的數(shù)字代碼。將此數(shù)字代碼傳遞給xml_error_string()函數(shù)即可得到錯誤的文本信息。輸出XML當(dāng)前的行數(shù),使得調(diào)試更容易。
當(dāng)解析文檔時(shí),對于Expat需要強(qiáng)調(diào)問題的是:如何保持文檔結(jié)構(gòu)的基本描述?
如前所述,基于事件的解析器本身并不產(chǎn)生任何結(jié)構(gòu)信息。不過標(biāo)簽(tag)結(jié)構(gòu)是XML的重要特性。例如,元素序列
為了產(chǎn)生文檔結(jié)構(gòu)的鏡像,腳本至少需要知道目前元素的父元素。用Exapt的API是無法實(shí)現(xiàn)的,它只報(bào)告目前元素的事件,而沒有任何前后關(guān)系的信息。因此,需要建立自己的棧結(jié)構(gòu)。
腳本范例使用先進(jìn)后出(FILO)的棧結(jié)構(gòu)。通過一個數(shù)組,棧將保存全部的開始元素。對于開始元素處理函數(shù),目前的元素將被array_push()函數(shù)推到棧的頂部。相應(yīng)的,結(jié)束元素處理函數(shù)通過array_pop()將最頂?shù)脑匾谱摺?
對于序列
開始元素book:將"book"賦給棧的第一個元素($stack[0])。
開始元素title:將"title"賦給棧的頂部($stack[1])。
結(jié)束元素title:從棧中將最頂部的元素移去($stack[1])。
結(jié)束元素title:從棧中將最頂部的元素移去($stack[0])。
PHP3.0通過一個$depth變量手動控制元素的嵌套來實(shí)現(xiàn)范例,這就使腳本看起來比較復(fù)雜。PHP4.0通過array_pop()和array_push()兩個函數(shù)來使腳本看起來更簡潔。
如何收集XML文檔中的元素信息?
為了收集每個元素的信息,腳本需要記住每個元素的事件。通過使用一個全局的數(shù)組變量$elements來保存文檔中所有不同的元素。數(shù)組的項(xiàng)目是元素類的實(shí)例,有4個屬性(類的變量)
$count -該元素在文檔中被發(fā)現(xiàn)的次數(shù)
$chars -元素中字符事件的字節(jié)數(shù)
$parents -父元素
$childs - 子元素
注意:PHP的一個特性是你可以通過while(list() = each())loop遍歷整個類結(jié)構(gòu),如同你遍歷整個相應(yīng)的數(shù)組一樣。所有的類變量(當(dāng)你用PHP3.0時(shí)還有方法名)都以字符串的方式輸出。
當(dāng)發(fā)現(xiàn)一個元素時(shí),我們需要增加其相應(yīng)的記數(shù)器來跟蹤它在文檔中出現(xiàn)多少次。在相應(yīng)的$elements項(xiàng)中的記數(shù)元素也要加一。
我們同樣要讓父元素知道目前的元素是它的子元素。因此,目前元素的名稱將會加入到父元素的$childs數(shù)組的項(xiàng)目中。最后,目前元素應(yīng)該記住誰是它的父元素。所以,父元素被加入到目前元素$parents數(shù)組的項(xiàng)目中。
顯示統(tǒng)計(jì)信息
剩下的代碼在$elements數(shù)組和其子數(shù)組中循環(huán)顯示其統(tǒng)計(jì)結(jié)果。這就是最簡單的嵌套循環(huán),盡管輸出正確的結(jié)果,但代碼既不簡潔又沒有任何特別的技巧,它僅僅是一個你可能每天用他來完成工作的循環(huán)。
腳本范例被設(shè)計(jì)為通過PHP的CGI方式的命令行來調(diào)用。因此,統(tǒng)計(jì)結(jié)果輸出的格式為文本格式。如果你要將腳本運(yùn)用到互聯(lián)網(wǎng)上,那么你需要修改輸出函數(shù)來產(chǎn)生HTML格式。
如何用PHP&XML編制一個迷你搜索引擎實(shí)例?
讓我們首先來熟悉一下我們程序中用到的那個XML(保存為xyz.xml)。
電腦網(wǎng)絡(luò)
程序設(shè)計(jì)
PHP
www.phpbuilder.com
PHP Manual
它的結(jié)構(gòu)相當(dāng)簡單,根元素就是links,sub代表著一個類別,web就是一個網(wǎng)站的信息,其中包含著屬性,url代表網(wǎng)站的聯(lián)接,memo為備注信息,
現(xiàn)在我們來回答上面提出的問題:為什么要用XML來編制搜索引擎?
第一個原因就是有時(shí)候由于各種原因我們可能不能用到數(shù)據(jù)庫(MySQL或者其他);
其次,對于小數(shù)據(jù)量的搜索引擎來說,它的數(shù)據(jù)量很小,如果用數(shù)據(jù)庫來做,效率未必有多高;
最重要的一點(diǎn)是,這個搜索引擎維護(hù)起來相當(dāng)?shù)暮唵?,并且不用編寫繁瑣的?shù)據(jù)庫的維護(hù)的程序。例如,我們要添加一個類別或者網(wǎng)頁,只要編輯文本的文件,加上一福紈eb>???或是????就可以了,而且,如果想把一個類別移動到另一個地方的話,我們只要將這一部分的sub復(fù)制過去就行了。
下面一個最簡單的用PHP顯示XML的范例。
下面的程序是將解析XML并按照樹形結(jié)構(gòu)輸出至瀏覽器,并顯示每層的元素總數(shù)。
$file = "demo.xml";// XML文件
function xml_parse_from_file($parser, $file) {// 解析XML文件的函數(shù) }
function start_element($parser, $name, $attrs) {//遇到了開元素標(biāo)記如就執(zhí)行這一段}
function stop_element($parser, $name) {//遇到了開元素標(biāo)記如

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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)

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

Using Python to implement data validation in XML Introduction: In real life, we often deal with a variety of data, among which XML (Extensible Markup Language) is a commonly used data format. XML has good readability and scalability, and is widely used in various fields, such as data exchange, configuration files, etc. When processing XML data, we often need to verify the data to ensure the integrity and correctness of the data. This article will introduce how to use Python to implement data verification in XML and give the corresponding
