自定義php類(查找/修改)xml文檔_PHP教程
推薦:PHP字符過濾函數(shù)去除字符串最后一個(gè)逗號(hào)(rtrim)PHP字符過濾函數(shù)去除字符串最后一個(gè)逗號(hào),用php自帶的函數(shù)比較容易解決
近期在看PHP的教學(xué)視頻,其中講到了 PHP 操作 xml 文檔,學(xué)了點(diǎn)兒 DOMDocument 類。自己查手冊(cè)又全英文,看不大懂。但還是自己寫了個(gè)類,實(shí)現(xiàn)了查找 xml 節(jié)點(diǎn),并修改節(jié)點(diǎn)值。背景解說完畢,且看代碼如下:復(fù)制代碼 代碼如下:hl5o.cn
/*
<?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中將html中的br換行符轉(zhuǎn)換為文本輸入中的換行符PHP中的有個(gè)非常好的函數(shù):nl2br(),將文本框中的換行轉(zhuǎn)換為HTML頁(yè)面的br /,但是如何實(shí)現(xiàn)將html中的br /換行符轉(zhuǎn)換為文本框中的換行符呢
相關(guān)PHP教程:
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁(yè)面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對(duì)圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級(jí)介紹
- 關(guān)于PHP語(yǔ)言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國(guó)語(yǔ)言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- 實(shí)例學(xué)習(xí)PHP如何實(shí)現(xiàn)在線發(fā)郵件
- 如何通過PHP實(shí)現(xiàn)DataGrid功能
- php解析html類庫(kù)simple_html_dom(詳細(xì)介紹)
- 如何實(shí)現(xiàn)給上傳圖片加水印
- 深入PHP操作MongoDB的技術(shù)總結(jié)
- PHP 登錄記住密碼實(shí)現(xiàn)思路
- 淺談PHP與C#的值類型指向區(qū)別的詳解
- 多核編程中的負(fù)載平衡難題
- 怎樣用PHP編程語(yǔ)言開發(fā)動(dòng)態(tài)WAP頁(yè)面
- php實(shí)現(xiàn)自動(dòng)獲取生成文章主題關(guān)鍵詞功能的深入分析
- 相關(guān)鏈接:
- 教程說明:
PHP教程-自定義php類(查找/修改)xml文檔
。