自定義php類(查找/修改)xml文檔
更新時間:2013年03月26日 09:54:41 作者:
看了下PHP操作xml文檔,學了點兒 DOMDocument 類,接下來為大家介紹自己寫個類,實現(xiàn)了查找 xml 節(jié)點,并修改節(jié)點值,感興趣的各位可以參考下
近期在看PHP的教學視頻,其中講到了 PHP 操作 xml 文檔,學了點兒 DOMDocument 類。自己查手冊又全英文,看不大懂。但還是自己寫了個類,實現(xiàn)了查找 xml 節(jié)點,并修改節(jié)點值。背景解說完畢,且看代碼如下:
/*
<?xml version="1.0" encoding="UTF-8"?>
<班級>
<學生 number="101">
<名字>孫悟空</名字>
<名字>孫行者</名字>
<年齡>猴精猴精</年齡>
<介紹></介紹>
</學生>
<學生 number="102">
<名字>白骨精</名字>
<年齡>140</年齡>
<介紹>幻化萬千</介紹>
</學生>
<學生 number="103">
<名字>豬八戒</名字>
<名字>豬無能</名字>
<年齡>200</年齡>
<介紹>能吃會睡</介紹>
</學生>
</班級>
*/
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é)點
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('學生','年齡','103');//找到 年齡=103 的豬八戒
$test->update('小豬豬', '名字', false, 1); //把豬八戒的第二個名字改成:小豬豬
$test->save('new.xml'); //保存成新文件
復制代碼 代碼如下:
/*
<?xml version="1.0" encoding="UTF-8"?>
<班級>
<學生 number="101">
<名字>孫悟空</名字>
<名字>孫行者</名字>
<年齡>猴精猴精</年齡>
<介紹></介紹>
</學生>
<學生 number="102">
<名字>白骨精</名字>
<年齡>140</年齡>
<介紹>幻化萬千</介紹>
</學生>
<學生 number="103">
<名字>豬八戒</名字>
<名字>豬無能</名字>
<年齡>200</年齡>
<介紹>能吃會睡</介紹>
</學生>
</班級>
*/
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é)點
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('學生','年齡','103');//找到 年齡=103 的豬八戒
$test->update('小豬豬', '名字', false, 1); //把豬八戒的第二個名字改成:小豬豬
$test->save('new.xml'); //保存成新文件
相關文章
PHP使用stream_context_create()模擬POST/GET請求的方法
這篇文章主要介紹了PHP使用stream_context_create()模擬POST/GET請求的方法,結合實例形式較為詳細的分析了stream_context_create模擬POST/GET請求的原理,使用方法與相關注意事項,需要的朋友可以參考下2016-04-04gearman中任務的優(yōu)先級和返回狀態(tài)實例分析
這篇文章主要介紹了gearman中任務的優(yōu)先級和返回狀態(tài),結合實例形式分析了gearman任務的優(yōu)先級以及獲取返回狀態(tài)相關操作技巧,需要的朋友可以參考下2020-02-02PHP實現(xiàn)可添加水印與生成縮略圖的圖片處理工具類
這篇文章主要介紹了PHP實現(xiàn)可添加水印與生成縮略圖的圖片處理工具類,涉及php針對圖片的顯示、保存、壓縮、水印等相關操作技巧,需要的朋友可以參考下2018-01-01