亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home php教程 php手冊(cè) 使用PHP 5.0 輕松解析XML文檔(1)

使用PHP 5.0 輕松解析XML文檔(1)

Jun 21, 2016 am 09:15 AM
echo font name xml

xml

用sax方式的時(shí)候,要自己構(gòu)建3個(gè)函數(shù),而且要直接用這三的函數(shù)來(lái)返回?cái)?shù)據(jù), 要求較強(qiáng)的邏輯。 在處理不同結(jié)構(gòu)的xml的時(shí)候, 還要重新進(jìn)行構(gòu)造這三個(gè)函數(shù),麻煩!

  用dom方式,倒是好些,但是他把每個(gè)節(jié)點(diǎn)都看作是一個(gè)node,操作起來(lái)要寫(xiě)好多的代碼, 麻煩!

  網(wǎng)上有好多的開(kāi)源的xml解析的類(lèi)庫(kù), 以前看過(guò)幾個(gè),但是心里總是覺(jué)得不踏實(shí),感覺(jué)總是跟在別人的屁股后面.

  這幾天在搞java, 挺累的,所以決定換換腦袋,寫(xiě)點(diǎn)php代碼,為了防止以后xml解析過(guò)程再令我犯難,就花了一天的時(shí)間寫(xiě)了下面一個(gè)xml解析的類(lèi),于是就有了下面的東西。

  實(shí)現(xiàn)方式是通過(guò)包裝"sax方式的解析結(jié)果"來(lái)實(shí)現(xiàn)的. 總的來(lái)說(shuō),對(duì)于我個(gè)人來(lái)說(shuō)挺實(shí)用的,性能也還可以,基本上可以完成大多數(shù)的處理要求。

  功能:

  1、 對(duì)基本的xml文件的節(jié)點(diǎn)進(jìn)行 查詢(xún) / 添加 / 修改 / 刪除 工作.

  2、導(dǎo)出xml文件的所有數(shù)據(jù)到一個(gè)數(shù)組里面.

  3、整個(gè)設(shè)計(jì)采用了oo方式,在操作結(jié)果集的時(shí)候, 使用方法類(lèi)似于dom

  缺點(diǎn):

  1、 每個(gè)節(jié)點(diǎn)最好都帶有一個(gè)id(看后面的例子), 每個(gè)“節(jié)點(diǎn)名字”=“節(jié)點(diǎn)的標(biāo)簽_節(jié)點(diǎn)的id”,如果這個(gè)id值沒(méi)有設(shè)置,程序?qū)⒆詣?dòng)給他產(chǎn)生一個(gè)id,這個(gè)id就是這個(gè)節(jié)點(diǎn)在他的上級(jí)節(jié)點(diǎn)中的位置編號(hào),從0開(kāi)始。

   2、 查詢(xún)某個(gè)節(jié)點(diǎn)的時(shí)候可以通過(guò)用“|”符號(hào)連接“節(jié)點(diǎn)名字”來(lái)進(jìn)行。這些“節(jié)點(diǎn)名字”都是按順序?qū)懞玫纳霞?jí)節(jié)點(diǎn)的名字。

  使用說(shuō)明:

  運(yùn)行下面的例子,在執(zhí)行結(jié)果頁(yè)面上可以看到函數(shù)的使用說(shuō)明

  代碼是通過(guò)php5來(lái)實(shí)現(xiàn)的,在php4中無(wú)法正常運(yùn)行。

  由于剛剛寫(xiě)完,所以沒(méi)有整理文檔,下面的例子演示的只是一部分的功能,代碼不是很難,要是想知道更多的功能,可以研究研究源代碼。

  目錄結(jié)構(gòu):

      test.php      test.xml      xml / SimpleDocumentBase.php      xml / SimpleDocumentNode.php      xml / SimpleDocumentRoot.php      xml / SimpleDocumentParser.php
  文件:test.xml

<?xml version="1.0" encoding="GB2312"?><br><shop><br> <name>華聯(lián)</name><br> <address>北京長(zhǎng)安街-9999號(hào)</address><br> <desc>連鎖超市</desc><br> <cat id="food"><br>  <goods id="food11"><br>   <name>food11</name><br>   <price>12.90</price><br>  </goods><br>  <goods id="food12"><br>   <name>food12</name><br>   <price>22.10</price><br>   <desc creator="hahawen">好東西推薦</desc><br>  </goods><br> </cat><br> <cat><br>  <goods id="tel21"><br>   <name>tel21</name><br>   <price>1290</price><br>  </goods><br> </cat><br> <cat id="coat"><br>  <goods id="coat31"><br>   <name>coat31</name><br>   <price>112</price><br>  </goods><br>  <goods id="coat32"><br>   <name>coat32</name><br>   <price>45</price><br>  </goods><br> </cat><br> <special id="hot"><br>  <goods><br>   <name>hot41</name><br>   <price>99</price><br>  </goods><br> </special><br></shop>

   文件:test.php

  <?php    require_once "xml/SimpleDocumentParser.php";<br>    require_once "xml/SimpleDocumentBase.php";<br>    require_once "xml/SimpleDocumentRoot.php";<br>    require_once "xml/SimpleDocumentNode.php";<br>   $test = new SimpleDocumentParser();<br>    $test->parse("test.xml");<br>    $dom = $test->getSimpleDocument();<br>    echo "<pre>";<br>    echo "<hr><font color=red>";<br>    echo "下面是通過(guò)函數(shù)getSaveData()返回的整個(gè)xml數(shù)據(jù)的數(shù)組";<br>    echo "</font><hr>";<br>    print_r($dom->getSaveData());<br>    echo "<hr><font color=red>";<br>    echo "下面是通過(guò)setValue()函數(shù),給給根節(jié)點(diǎn)添加信息,添加后顯示出結(jié)果xml文件的內(nèi)容";<br>    echo "</font><hr>";<br>    $dom->setValue("telphone", "123456789");<br>    echo htmlspecialchars($dom->getSaveXml());<br>    echo "<hr><font color=red>";<br>    echo "下面是通過(guò)getNode()函數(shù),返回某一個(gè)分類(lèi)下的所有商品的信息";<br>    echo "</font><hr>";<br>    $obj = $dom->getNode("cat_food");<br>    $nodeList = $obj->getNode();<br>    foreach($nodeList as $node){<br>    $data = $node->getValue();<br>    echo "<font color=red>商品名:".$data[name]."</font><br>";<br>    print_R($data);<br>    print_R($node->getAttribute());<br>    }<br>    echo "<hr><font color=red>";<br>    echo "下面是通過(guò)findNodeByPath()函數(shù),返回某一商品的信息";<br>    echo "</font><hr>";<br>    $obj = $dom->findNodeByPath("cat_food|goods_food11");<br>    if(!is_object($obj)){<br>    echo "該商品不存在";<br>    }else{<br>    $data = $obj->getValue();<br>    echo "<font color=red>商品名:".$data[name]."</font><br>";<br>    print_R($data);<br>    print_R($obj->getAttribute());<br>    }<br>   echo "<hr><font color=red>";<br>    echo "下面是通過(guò)setValue()函數(shù),給商品\"food11\"添加屬性, 然后顯示添加后的結(jié)果";<br>    echo "</font><hr>";<br>    $obj = $dom->findNodeByPath("cat_food|goods_food11");<br>    $obj->setValue("leaveword", array("value"=>"這個(gè)商品不錯(cuò)",<br>          "attrs"=>array("author"=>"hahawen", "date"=>date('Y-m-d'))));<br>    echo htmlspecialchars($dom->getSaveXml());<br>    echo "<hr><font color=red>";<br>    echo "下面是通過(guò)removeValue()/removeAttribute()函數(shù),<br>          給商品\"food11\"改變和刪除屬性, 然后顯示操作后的結(jié)果";<br>    echo "</font><hr>";<br>    $obj = $dom->findNodeByPath("cat_food|goods_food12");<br>    $obj->setValue("name", "new food12");<br>    $obj->removeValue("desc");<br>    echo htmlspecialchars($dom->getSaveXml());<br>    echo "<hr><font color=red>";<br>    echo "下面是通過(guò)createNode()函數(shù),添加商品, 然后顯示添加后的結(jié)果";<br>    echo "</font><hr>";<br>    $obj = $dom->findNodeByPath("cat_food");<br>    $newObj = $obj->createNode("goods", array("id"=>"food13"));<br>    $newObj->setValue("name", "food13");<br>    $newObj->setValue("price", 100);<br>    echo htmlspecialchars($dom->getSaveXml());<br>    echo "<hr><font color=red>";<br>    echo "下面是通過(guò)removeNode()函數(shù),刪除商品, 然后顯示刪除后的結(jié)果";<br>    echo "</font><hr>";<br>    $obj = $dom->findNodeByPath("cat_food");<br>    $obj->removeNode("goods_food12");<br>    echo htmlspecialchars($dom->getSaveXml());<br>  ?>



Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

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

Convert XML data to CSV format in Python Convert XML data to CSV format in Python Aug 11, 2023 pm 07:41 PM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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 How to handle XML and JSON data formats in C# development Oct 09, 2023 pm 06:15 PM

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

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

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 verification in XML Using Python to implement data verification in XML Aug 10, 2023 pm 01:37 PM

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

Convert POJO to XML using Jackson library in Java? Convert POJO to XML using Jackson library in Java? Sep 18, 2023 pm 02:21 PM

Jackson is a Java-based library that is useful for converting Java objects to JSON and JSON to Java objects. JacksonAPI is faster than other APIs, requires less memory area, and is suitable for large objects. We use the writeValueAsString() method of the XmlMapper class to convert the POJO to XML format, and the corresponding POJO instance needs to be passed as a parameter to this method. Syntax publicStringwriteValueAsString(Objectvalue)throwsJsonProcessingExceptionExampleimp

What are the most popular golang frameworks on the market? What are the most popular golang frameworks on the market? Jun 01, 2024 pm 08:05 PM

The most popular Go frameworks at present are: Gin: lightweight, high-performance web framework, simple and easy to use. Echo: A fast, highly customizable web framework that provides high-performance routing and middleware. GorillaMux: A fast and flexible multiplexer that provides advanced routing configuration options. Fiber: A performance-optimized, high-performance web framework that handles high concurrent requests. Martini: A modular web framework with object-oriented design that provides a rich feature set.

See all articles