佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > 使用php操作xml教程

使用php操作xml教程

熱門標(biāo)簽:團(tuán)購(gòu)網(wǎng)站 電子圍欄 阿里云 科大訊飛語(yǔ)音識(shí)別系統(tǒng) Mysql連接數(shù)設(shè)置 Linux服務(wù)器 服務(wù)器配置 銀行業(yè)務(wù)

php操作xml

最近計(jì)劃寫個(gè)人的小網(wǎng)站,一系列原因選擇了用php來(lái)寫,最大的問(wèn)題就是雖然php很流行,但我從來(lái)沒有接觸過(guò)php,看了一個(gè)多星期的基本語(yǔ)法后做些小練習(xí)熱熱身,但是期間是各種問(wèn)題啊,主要是對(duì)php不熟悉,遇到一些總結(jié)一些吧。

數(shù)據(jù)

?xml version="1.0"?>
books>
    book name="JavaScript: The Defiitive Guide" publisher="O'Reilly Media, Inc.">
        author>David Flanagan/author>
    /book>
    book name="PHP anf MySQL Web Development" publisher="Perason Education">
        author>Luke Welling/author>
        author>Laura Thomson/author>
    /book>
    book name="HTTP: The Defiitive Guide" publisher="O'Reilly Media, Inc.">
        author>David Courley/author>
        author>Brian Totty/author>
    /book>
/books>

XML幾個(gè)基本概念

節(jié)點(diǎn):節(jié)點(diǎn)也就是很多程序語(yǔ)言中處理XML時(shí)的Node,節(jié)點(diǎn)是一個(gè)比較寬泛的概念,在XML中元素,屬性,名字空間,注釋,文本內(nèi)容,處理指令,還有整個(gè)文檔都屬于節(jié)點(diǎn),也就是說(shuō)XML文檔中每個(gè)獨(dú)立的一小部分都是節(jié)點(diǎn),是,也是,name=”XXXX”也是,標(biāo)簽是,甚至作者的名字David Flanagan都是一個(gè)文本節(jié)點(diǎn)。

元素:很多程序語(yǔ)言都有對(duì)XML處理,節(jié)點(diǎn)是一個(gè)很寬泛的概念,因?yàn)橐y(tǒng)一API,對(duì)節(jié)點(diǎn)不會(huì)有過(guò)多方法,而元素也就是Element是節(jié)點(diǎn)的一個(gè)子集,簡(jiǎn)單講就是這樣的標(biāo)簽才算,一般會(huì)有很多針對(duì)元素的操作方法。

屬性:這個(gè)比較好理解,在>里面的類似XX=”O(jiān)O”等東西都是屬性節(jié)點(diǎn)

轉(zhuǎn)義字符:和HTML等類似,xml也有語(yǔ)言占用的符號(hào),想使用的這些特殊字符的時(shí)候需要轉(zhuǎn)義

lt;

>

gt;

apos;

quot;

DOMDocument對(duì)象

我使用的是DOMDocument對(duì)象來(lái)操作xml,感覺用起來(lái)比simpleXml科學(xué)一些,當(dāng)然第一天使用php,純屬個(gè)人感覺。DOMDocument有幾個(gè)常用的屬性和方法。

屬性 作用
attributes 節(jié)點(diǎn)屬性集合
parentNode 節(jié)點(diǎn)父節(jié)點(diǎn)
documentElement 文檔根節(jié)點(diǎn)
nodeName 節(jié)點(diǎn)的名字
nodeType 節(jié)點(diǎn)類型
nodeValue 節(jié)點(diǎn)值
Text 節(jié)點(diǎn)及其子節(jié)點(diǎn)轉(zhuǎn)換為文字
方法 作用
appendChild 為節(jié)點(diǎn)添加子節(jié)點(diǎn)
createAttribute 創(chuàng)建屬性節(jié)點(diǎn)
createElement 創(chuàng)建元素
getElementsByTagName 通過(guò)節(jié)點(diǎn)名獲取節(jié)點(diǎn)集合
hasChildNodes 判斷節(jié)點(diǎn)是否有子節(jié)點(diǎn)
insertBefore 在節(jié)點(diǎn)
Load 通過(guò)文檔路徑加載xml
loadXML 加載zml字符串
removeChild 刪除子節(jié)點(diǎn)
removeAttribute 刪除屬性節(jié)點(diǎn)
save 保存文檔

加載xml

$path=$_SERVER["DOCUMENT_ROOT"].'/books.xml';
    $books=new DOMDocument();
    $books->load($path);

讀取/遍歷節(jié)點(diǎn)與屬性

$bookElements=$books->getElementsByTagName('book');

    foreach($bookElements as $book){
        foreach ($book->attributes as $attr) {
            echo strtoupper($attr->nodeName).' —— '.$attr->nodeValue.'br/>';
        }
        echo "AUTHOR: ";
        foreach ($book->getElementsByTagName('author') as $author) {
            echo $author->nodeValue.' ';
        }
        echo 'br/>br/>';
    }

當(dāng)然對(duì)于很多屬性,只想讀一個(gè),可以通過(guò)item(index)方法按索引讀取

echo $book->attributes->item(1)->nodeValue;

還可以通過(guò)強(qiáng)大的xpath查詢

$xpath = new domxpath($books);
$bookElements=$xpath->query("/books/book");

修改屬性/節(jié)點(diǎn)

foreach($bookElements as $book){
        foreach ($book->attributes as $attr) {
            #$book->setAttribute($attr->nodeName,strtoupper($attr->nodeValue));
            $attr->nodeValue=strtoupper($attr->nodeValue);
        }
        echo "AUTHOR: ";
        foreach ($book->getElementsByTagName('author') as $author) {
            $author->nodeValue=strtoupper($author->nodeValue);
        }

    }
    $books->save($path);

對(duì)屬性修改可以直接訪問(wèn)其nodeValue改動(dòng),也可以使用setAttribute方法,改動(dòng)完了別忘了使用save保存。

$book->setAttribute($attr->nodeName,strtoupper($attr->nodeValue));
$attr->nodeValue=strtoupper($attr->nodeValue);

添加元素/屬性

$newBook=$books->createElement('book'); #創(chuàng)建新元素
    $newBook->setAttribute('name','PHP Objects, Patterns, and Practice');#創(chuàng)建新屬性,方法一

    $publisher=$books->createAttribute('publisher');#創(chuàng)建新屬性,方法二
    $publisher->nodeValue='Apress L.P';
    $newBook->appendChild($publisher); #把屬性添加到元素上

    $author=$books->createElement('author');#創(chuàng)建子元素
    $author->nodeValue='Matt Zandstra';
    $newBook->appendChild($author);#把子元素添加到父元素上

    $books->documentElement->appendChild($newBook);#添加整個(gè)節(jié)點(diǎn)
    $books->save($path);

刪除屬性/節(jié)點(diǎn)

$first=$bookElements->item(0);
    $first->removeAttribute('publisher');

    $second=$bookElements->item(1);
    $second->parentNode->removeChild($second);

    $books->save($path);

到此這篇關(guān)于使用php操作xml教程的文章就介紹到這了,更多相關(guān)php操作xml內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • linux下php安裝xml擴(kuò)展的詳細(xì)步驟
  • php 使用expat方式解析xml文件操作示例
  • php實(shí)現(xiàn)的數(shù)組轉(zhuǎn)xml案例分析
  • PHP讀取XML文件的方法實(shí)例總結(jié)【DOMDocument及simplexml方法】
  • PHP創(chuàng)建XML的方法示例【基于DOMDocument類及SimpleXMLElement類】

標(biāo)簽:衡水 蚌埠 大理 棗莊 廣元 衢州 萍鄉(xiāng) 江蘇

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用php操作xml教程》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    江北区| 古交市| 凌云县| 集贤县| 北海市| 瑞丽市| 哈尔滨市| 农安县| 汉川市| 大余县| 溧阳市| 河间市| 社旗县| 象山县| 新津县| 宿州市| 独山县| 丽江市| 黔江区| 札达县| 双鸭山市| 曲水县| 渭源县| 新源县| 神池县| 嵊州市| 电白县| 昌平区| 军事| 夏河县| 于田县| 施甸县| 石泉县| 渭南市| 赤水市| 阿荣旗| 广州市| 改则县| 成安县| 鄂伦春自治旗| 高清|