最近計劃寫個人的小網(wǎng)站,一系列原因選擇了用php來寫,最大的問題就是雖然php很流行,但我從來沒有接觸過php,看了一個多星期的基本語法后做些小練習(xí)熱熱身,但是期間是各種問題啊,主要是對php不熟悉,遇到一些總結(jié)一些吧。
?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>
節(jié)點:節(jié)點也就是很多程序語言中處理XML時的Node,節(jié)點是一個比較寬泛的概念,在XML中元素,屬性,名字空間,注釋,文本內(nèi)容,處理指令,還有整個文檔都屬于節(jié)點,也就是說XML文檔中每個獨立的一小部分都是節(jié)點,
元素:很多程序語言都有對XML處理,節(jié)點是一個很寬泛的概念,因為要統(tǒng)一API,對節(jié)點不會有過多方法,而元素也就是Element是節(jié)點的一個子集,簡單講就是
屬性:這個比較好理解,在>里面的類似XX=”O(jiān)O”等東西都是屬性節(jié)點
轉(zhuǎn)義字符:和HTML等類似,xml也有語言占用的符號,想使用的這些特殊字符的時候需要轉(zhuǎn)義
lt; |
|
> |
gt; |
‘ |
apos; |
“ |
quot; |
我使用的是DOMDocument對象來操作xml,感覺用起來比simpleXml科學(xué)一些,當(dāng)然第一天使用php,純屬個人感覺。DOMDocument有幾個常用的屬性和方法。
屬性 | 作用 |
attributes | 節(jié)點屬性集合 |
parentNode | 節(jié)點父節(jié)點 |
documentElement | 文檔根節(jié)點 |
nodeName | 節(jié)點的名字 |
nodeType | 節(jié)點類型 |
nodeValue | 節(jié)點值 |
Text | 節(jié)點及其子節(jié)點轉(zhuǎn)換為文字 |
方法 | 作用 |
appendChild | 為節(jié)點添加子節(jié)點 |
createAttribute | 創(chuàng)建屬性節(jié)點 |
createElement | 創(chuàng)建元素 |
getElementsByTagName | 通過節(jié)點名獲取節(jié)點集合 |
hasChildNodes | 判斷節(jié)點是否有子節(jié)點 |
insertBefore | 在節(jié)點 |
Load | 通過文檔路徑加載xml |
loadXML | 加載zml字符串 |
removeChild | 刪除子節(jié)點 |
removeAttribute | 刪除屬性節(jié)點 |
save | 保存文檔 |
$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/>'; }
當(dāng)然對于很多屬性,只想讀一個,可以通過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);
對屬性修改可以直接訪問其nodeValue改動,也可以使用setAttribute方法,改動完了別忘了使用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);#添加整個節(jié)點 $books->save($path);
$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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
標(biāo)簽:赤峰 溫州 怒江 金華 酒泉 洛陽 七臺河 白城
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用php操作xml教程》,本文關(guān)鍵詞 使用,php,操作,xml,教程,使用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。上一篇:php的Snoopy類案例講解
下一篇:實例分析php常量和變量的不同