使用php操作xml教程
php操作xml
最近計(jì)劃寫(xiě)個(gè)人的小網(wǎng)站,一系列原因選擇了用php來(lái)寫(xiě),最大的問(wèn)題就是雖然php很流行,但我從來(lái)沒(méi)有接觸過(guò)php,看了一個(gè)多星期的基本語(yǔ)法后做些小練習(xí)熱熱身,但是期間是各種問(wèn)題啊,主要是對(duì)php不熟悉,遇到一些總結(jié)一些吧。
數(shù)據(jù)
<?xml version="1.0"?>javaScript: The Defiitive Guide" publisher="O'Reilly Media, Inc."> David Flanagan Luke Welling Laura Thomson David Courley Brian Totty
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),
元素:很多程序語(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)單講就是
屬性:這個(gè)比較好理解,在<>里面的類似XX=”O(jiān)O”等東西都是屬性節(jié)點(diǎn)
轉(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
$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.'
'; } echo "AUTHOR: "; foreach ($book->getElementsByTagName('author') as $author) { echo $author->nodeValue.' '; } echo '
'; }
當(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)文章希望大家以后多多支持腳本之家!
版權(quán)聲明:
本站所有文章和圖片均來(lái)自用戶分享和網(wǎng)絡(luò)收集,文章和圖片版權(quán)歸原作者及原出處所有,僅供學(xué)習(xí)與參考,請(qǐng)勿用于商業(yè)用途,如果損害了您的權(quán)利,請(qǐng)聯(lián)系網(wǎng)站客服處理。