Custom php class of looks for and to modify xml documents

  • 2020-05-30 19:43:22
  • OfStack

Recently, I watched the teaching video of PHP, in which I talked about PHP operation xml document and learned a little about DOMDocument class. I can't understand the manual because it's all in English. However, I still wrote a class to find the xml node and modify the value of the node. The background explanation is finished, and the code is as follows:
 
/* 
<?xml version="1.0" encoding="UTF-8"?> 
< The class > 
< students  number="101"> 
< The name > The Monkey King </ The name > 
< The name > Sun walkers </ The name > 
< age > A clear and mischievous person a clear and mischievous person </ age > 
< introduce ></ introduce > 
</ students > 
< students  number="102"> 
< The name > Bones jing </ The name > 
< age >140</ age > 
< introduce > Form ten thousand </ introduce > 
</ students > 
< students  number="103"> 
< The name > The pig 8 quit </ The name > 
< The name > Pigs can </ The name > 
< age >200</ age > 
< introduce > Can eat can sleep </ introduce > 
</ students > 
</ The class > 
*/ 
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);// The element  
$node = $item->getElementsByTagName($seachNode);// node  
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(' students ',' age ','103');// find   age =103  The pig 8 quit  
$test->update(' The little pig ', ' The name ', false, 1); // The pig 8 To quit the first 2 Name change: small pig pig  
$test->save('new.xml'); // Save to a new file  

Related articles: