自定義php類(查找/修改)xml文檔
更新時(shí)間:2013年03月26日 09:54:41 作者:
看了下PHP操作xml文檔,學(xué)了點(diǎn)兒 DOMDocument 類,接下來(lái)為大家介紹自己寫個(gè)類,實(shí)現(xiàn)了查找 xml 節(jié)點(diǎn),并修改節(jié)點(diǎn)值,感興趣的各位可以參考下
近期在看PHP的教學(xué)視頻,其中講到了 PHP 操作 xml 文檔,學(xué)了點(diǎn)兒 DOMDocument 類。自己查手冊(cè)又全英文,看不大懂。但還是自己寫了個(gè)類,實(shí)現(xiàn)了查找 xml 節(jié)點(diǎn),并修改節(jié)點(diǎn)值。背景解說(shuō)完畢,且看代碼如下:
/*
<?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'); //保存成新文件
復(fù)制代碼 代碼如下:
/*
<?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'); //保存成新文件
您可能感興趣的文章:
相關(guān)文章
PHP使用stream_context_create()模擬POST/GET請(qǐng)求的方法
這篇文章主要介紹了PHP使用stream_context_create()模擬POST/GET請(qǐng)求的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了stream_context_create模擬POST/GET請(qǐng)求的原理,使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-04-04gearman中任務(wù)的優(yōu)先級(jí)和返回狀態(tài)實(shí)例分析
這篇文章主要介紹了gearman中任務(wù)的優(yōu)先級(jí)和返回狀態(tài),結(jié)合實(shí)例形式分析了gearman任務(wù)的優(yōu)先級(jí)以及獲取返回狀態(tài)相關(guān)操作技巧,需要的朋友可以參考下2020-02-02PHP實(shí)現(xiàn)可添加水印與生成縮略圖的圖片處理工具類
這篇文章主要介紹了PHP實(shí)現(xiàn)可添加水印與生成縮略圖的圖片處理工具類,涉及php針對(duì)圖片的顯示、保存、壓縮、水印等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01