自定義php類(lèi)(查找/修改)xml文檔
/*
<?xml version="1.0" encoding="UTF-8"?>
<班級(jí)>
<學(xué)生 number="101">
<名字>孫悟空</名字>
<名字>孫行者</名字>
<年齡>猴精猴精</年齡>
<介紹></介紹>
</學(xué)生>
<學(xué)生 number="102">
<名字>白骨精</名字>
<年齡>140</年齡>
<介紹>幻化萬(wàn)千</介紹>
</學(xué)生>
<學(xué)生 number="103">
<名字>豬八戒</名字>
<名字>豬無(wú)能</名字>
<年齡>200</年齡>
<介紹>能吃會(huì)睡</介紹>
</學(xué)生>
</班級(jí)>
*/
class xmlDom{
public $version;
public $encoding;
private $xml;
private $items;
private $seachNode = '';
private $seachItem = '';
private $seachValue = '';
public $writeBytes = 0;
function __construct($xmlFile ='', $version ='1.0', $encoding = 'UTF-8'){
$this->version = $version;
$this->encoding = $encoding;
$this->xml = new DOMDocument($version, $encoding);
if($xmlFile)$this->xml->load($xmlFile);
}
function getRootEle($rootTag){
$this->xmlRoot = $this->xml->getElementsByTagName($rootTag)->item(0);
}
function getSeachItem($itemsTag, $seachNode, $seachValue){
$this->items = $this->xml->getElementsByTagName($itemsTag);
$this->items->length;
for($i=0; $i<$this->items->length; $i++){
$item = $this->items->item($i);//元素
$node = $item->getElementsByTagName($seachNode);//節(jié)點(diǎn)
for($j = 0; $j< $node->length; $j++){
$subNode = $node->item($j);
if($seachValue == $subNode->nodeValue){
$this->seachNode = $subNode;
$this->seachItem = $item;
$this->seachValue = $subNode->nodeValue;
break(2);
}
}
}
return ($this->seachNode) ? true : false;
}
function update($nodeValue, $nodeTag = '',$append = false, $index = 0){
if($append){
if($nodeTag)
$this->seachItem->getElementsByTagName($nodeTag)->item($index)->nodeValue += $nodeValue;
else
$this->seachNode->nodeValue += $nodeValue;
}else{
if($nodeTag)
$this->seachItem->getElementsByTagName($nodeTag)->item($index)->nodeValue = $nodeValue;
else
$this->seachNode->nodeValue = $nodeValue;
}
}
function save($filename){
$this->writeBytes = $this->xml->save($filename);
return ($this->writeBytes) ? true : false;
}
}
$test = new xmlDom('student.xml');
$test->getSeachItem('學(xué)生','年齡','103');//找到 年齡=103 的豬八戒
$test->update('小豬豬', '名字', false, 1); //把豬八戒的第二個(gè)名字改成:小豬豬
$test->save('new.xml'); //保存成新文件
- PHP遍歷XML文檔所有節(jié)點(diǎn)的方法
- 通過(guò)php修改xml文檔內(nèi)容的方法
- php中DOMElement操作xml文檔實(shí)例演示
- 通過(guò)php添加xml文檔內(nèi)容的方法
- 使用PHP和XSL stylesheets轉(zhuǎn)換XML文檔
- 通過(guò)php刪除xml文檔內(nèi)容的方法
- PHP基于DOM創(chuàng)建xml文檔的方法示例
- PHP使用DOM和simplexml讀取xml文檔的方法示例
- PHP4和PHP5版本下解析XML文檔的操作方法實(shí)例分析
- PHP實(shí)現(xiàn)動(dòng)態(tài)創(chuàng)建XML文檔的方法
相關(guān)文章
PHP 請(qǐng)求上下文相關(guān)總結(jié)
這篇文章主要介紹了PHP 請(qǐng)求上下文的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用PHP,感興趣的朋友可以了解下2021-04-04
基于PHP安裝zip拓展,以及l(fā)ibzip安裝的問(wèn)題
下面小編就為大家分享一篇基于PHP安裝zip拓展,以及l(fā)ibzip安裝的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
PHP中使用數(shù)組指針函數(shù)操作數(shù)組示例
這篇文章主要介紹了PHP中使用數(shù)組指針函數(shù)操作數(shù)組示例,本文講解了current()、key()、next()、prev()、end()、reset()等數(shù)組指針函數(shù),需要的朋友可以參考下2014-11-11
深入理解PHP中的static和yield關(guān)鍵字
這篇文章主要給大家介紹了關(guān)于PHP中static和yield關(guān)鍵字的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用php具有一定的參考學(xué)習(xí)價(jià)值,文章需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
PHP遍歷某個(gè)目錄下的所有文件和子文件夾的實(shí)現(xiàn)代碼
本篇文章是對(duì)PHP遍歷某個(gè)目錄下的所有文件和子文件夾的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP寫(xiě)的加密函數(shù),支持私人密鑰(詳細(xì)介紹)
本篇文章是對(duì)PHP寫(xiě)的加密函數(shù)且支持私人密鑰進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
PHP的Socket網(wǎng)絡(luò)編程入門(mén)指引
這篇文章主要介紹了PHP的Socket網(wǎng)絡(luò)編程入門(mén)指引,是PHP入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-08-08
PHP實(shí)現(xiàn)普通和定時(shí)跳轉(zhuǎn)的幾種方式
這篇文章主要為大家詳細(xì)介紹了PHP實(shí)現(xiàn)普通和定時(shí)跳轉(zhuǎn)的幾種方式,文章通過(guò)代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的小伙伴可以參考下2023-11-11
PHP環(huán)形鏈表實(shí)現(xiàn)方法示例
這篇文章主要介紹了PHP環(huán)形鏈表實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式分析了PHP環(huán)形鏈表的定義、創(chuàng)建及遍歷等操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-09-09
PHP基于數(shù)組實(shí)現(xiàn)的分頁(yè)函數(shù)實(shí)例
這篇文章主要介紹了PHP基于數(shù)組實(shí)現(xiàn)的分頁(yè)函數(shù)實(shí)例,是非常實(shí)用的一個(gè)技巧,需要的朋友可以參考下2014-08-08

