php操作xml


最近計划寫個人的小網站,一系列原因選擇了用php來寫,最大的問題就是雖然php很流行,但我從來沒有接觸過php,看了一個多星期的基本語法后做些小練習熱熱身,但是期間是各種問題啊,主要是對php不熟悉,遇到一些總結一些吧。

數據

<?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幾個基本概念

節點:節點也就是很多程序語言中處理XML時的Node,節點是一個比較寬泛的概念,在XML中元素,屬性,名字空間,注釋,文本內容,處理指令,還有整個文檔都屬於節點,也就是說XML文檔中每個獨立的一小部分都是節點,<books></books>是,<?xml version=”1.0”?>也是,name=”XXXX”也是,<author></author>標簽是,甚至作者的名字David Flanagan都是一個文本節點。

 

元素:很多程序語言都有對XML處理,節點是一個很寬泛的概念,因為要統一API,對節點不會有過多方法,而元素也就是Element是節點的一個子集,簡單講就是<xxx></xxx>這樣的標簽才算,一般會有很多針對元素的操作方法。

 

屬性:這個比較好理解,在<>里面的類似XX=”OO”等東西都是屬性節點

 

轉義字符:和HTML等類似,xml也有語言占用的符號,想使用的這些特殊字符的時候需要轉義

 

<

&lt;

>

&gt;

&

&amp;

&apos;

&quot;

 

DOMDocument對象

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

屬性 作用
attributes 節點屬性集合
parentNode
節點父節點
documentElement 文檔根節點
nodeName 節點的名字
nodeType 節點類型
nodeValue 節點值
Text 節點及其子節點轉換為文字

 

 

方法 作用
appendChild 為節點添加子節點
createAttribute 創建屬性節點
createElement 創建元素
getElementsByTagName 通過節點名獲取節點集合
hasChildNodes 判斷節點是否有子節點
insertBefore 在節點
Load 通過文檔路徑加載xml
loadXML 加載zml字符串
removeChild 刪除子節點
removeAttribute 刪除屬性節點
save 保存文檔

 

加載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.'&emsp;';
        }
        echo '<br/><br/>';
    }

image

 

 

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

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

 

還可以通過強大的xpath查詢

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

 

修改屬性/節點

 

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

 

image 對屬性修改可以直接訪問其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);

 

image

最后

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM