色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

php操作xml

要操作的數(shù)據(jù)
復(fù)制代碼 代碼如下:
<?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è)基本概念
1、 節(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),<books></books>是,<?xml version=”1.0”?>也是,name=”XXXX”也是,<author></author>標(biāo)簽是,甚至作者的名字David Flanagan都是一個(gè)文本節(jié)點(diǎn)。
2、元素:很多程序語(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)單講就是<xxx></xxx>這樣的標(biāo)簽才算,一般會(huì)有很多針對(duì)元素的操作方法。
3、屬性:這個(gè)比較好理解,在<>里面的類似XX=”O(jiān)O”等東西都是屬性節(jié)點(diǎn)
4、轉(zhuǎn)義字符:和HTML等類似,xml也有語(yǔ)言占用的符號(hào),想使用的這些特殊字符的時(shí)候需要轉(zhuǎn)義

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


加載xml
復(fù)制代碼 代碼如下:
$path=$_SERVER["DOCUMENT_ROOT"].'/books.xml';
    $books=new DOMDocument();
    $books->load($path);

讀取/遍歷節(jié)點(diǎn)與屬性
復(fù)制代碼 代碼如下:
$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)方法按索引讀取
復(fù)制代碼 代碼如下:
echo $book->attributes->item(1)->nodeValue;

還可以通過(guò)強(qiáng)大的xpath查詢
復(fù)制代碼 代碼如下:
還可以通過(guò)強(qiáng)大的xpath查詢

修改屬性/節(jié)點(diǎn)
復(fù)制代碼 代碼如下:
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保存。
復(fù)制代碼 代碼如下:
$book->setAttribute($attr->nodeName,strtoupper($attr->nodeValue));
$attr->nodeValue=strtoupper($attr->nodeValue);

添加元素/屬性
復(fù)制代碼 代碼如下:
$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)
復(fù)制代碼 代碼如下:
$first=$bookElements->item(0);
    $first->removeAttribute('publisher');

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

    $books->save($path);




初學(xué)php文章肯定有很多謬誤,希望大家批評(píng)指正,共同進(jìn)步。

php技術(shù)php操作xml,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 龙门县| 濉溪县| 潮安县| 肥西县| 贵德县| 迁西县| 丰宁| 离岛区| 凤翔县| 拉萨市| 湟源县| 师宗县| 阿拉尔市| 易门县| 静安区| 仁寿县| 丰宁| 玉龙| 沙田区| 介休市| 清涧县| 工布江达县| 湛江市| 五指山市| 绥江县| 资溪县| 深圳市| 朔州市| 铜山县| 庄河市| 宜宾市| 临猗县| 遂平县| 文化| 合山市| 佛学| 河西区| 龙岩市| 吉首市| 江孜县| 八宿县|