php操作xml_php執行個體

來源:互聯網
上載者:User

要操作的資料

複製代碼 代碼如下:

<?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幾個基本概念
1、 節點:節點也就是很多程式語言中處理XML時的Node,節點是一個比較寬泛的概念,在XML中元素,屬性,名字空間,注釋,常值內容,處理指示,還有整個文檔都屬於節點,也就是說XML文檔中每個獨立的一小部分都是節點,<books></books>是,<?xml version=”1.0”?>也是,name=”XXXX”也是,<author></author>標籤是,甚至作者的名字David Flanagan都是一個文本節點。
2、元素:很多程式語言都有對XML處理,節點是一個很寬泛的概念,因為要統一API,對節點不會有過多方法,而元素也就是Element是節點的一個子集,簡單講就是<xxx></xxx>這樣的標籤才算,一般會有很多針對元素的操作方法。
3、屬性:這個比較好理解,在<>裡面的類似XX=”OO”等東西都是屬性節點
4、逸出字元:和HTML等類似,xml也有語言佔用的符號,想使用的這些特殊字元的時候需要轉義

DOMDocument對象
我使用的是DOMDocument對象來操作xml,感覺用起來比simpleXml科學一些,當然第一天使用php,純屬個人感覺。DOMDocument有幾個常用的屬性和方法。


載入xml

複製代碼 代碼如下:

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

讀取/遍曆節點與屬性

複製代碼 代碼如下:

$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/>';
    }




當然對於很多屬性,只想讀一個,可以通過item(index)方法按索引讀取

複製代碼 代碼如下:

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

還可以通過強大的xpath查詢
複製代碼 代碼如下:

還可以通過強大的xpath查詢

修改屬性/節點

複製代碼 代碼如下:

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);



對屬性修改可以直接存取其nodeValue改動,也可以使用setAttribute方法,改動完了別忘了使用save儲存。

複製代碼 代碼如下:

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

添加元素/屬性

複製代碼 代碼如下:

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

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

    $author=$books->createElement('author');#建立子項目
    $author->nodeValue='Matt Zandstra';
    $newBook->appendChild($author);#把子項目添加到父元素上

    $books->documentElement->appendChild($newBook);#添加整個節點
    $books->save($path);

刪除屬性/節點

複製代碼 代碼如下:

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

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

    $books->save($path);





初學php文章肯定有很多謬誤,希望大家批評指正,共同進步。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.