The recent reading of PHP teaching video, which talked about the PHP operation of XML documents, learned a little DOMDocument class. I look at the manual and all English, do not understand. However, you have written a class that implements the lookup XML node and modifies the node value. The background explanation is complete, and the code reads as follows:
Copy Code code as follows:
/*
<?xml version= "1.0" encoding= "UTF-8"?>
< class >
< students ' "number=" >
< name > Monkey King </name >
< name > Sun Xing </name >
< age > Monkey Essence </Age >
< introduction ></>
</Students >
< student number= "102" >
< name > Genie </name >
< age >140</Age >
< introduction > Myriad </Introduction >
</Students >
< students ' "number=" >
< name > Pig </name >
< name > Pig incompetent </name >
< age >200</Age >
< introduction > Can eat Sleep </introduction >
</Students >
</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);//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 (' Student ', ' age ', ' 103 ')/find the pig of age =103
$test->update (' Piggy Pig ', ' name ', false, 1); To change the second name of the pig's eight-ring: Pig pig
$test->save (' new.xml '); Save as a new file