In the recent reading of PHP teaching video, which referred to the PHP operation of XML documents, learned a little DOMDocument class. I look at the manual and all English, do not understand. But also wrote a class, implemented the Find XML node, and modify the node value. Background commentary is complete, and look at the following code:
Copy CodeThe code is as follows:
/*
<班级> <班级>
<学生 number="101">
<名字> Monkey king
<名字> Sun Xing People
<年龄> Monkey Essence Monkey Essence
<介绍>
<学生 number="102">
<名字> Bones Jing
<年龄> 140
<介绍> The illusion of thousands
<学生 number="103">
<名字> Pig
<名字> Pig incompetence
<年龄> 200
<介绍> You can eat and sleep.
*/
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 Age =103 pig
$test->update (' pig ', ' name ', false, 1); Change the second name of the pig's Eight commandments to: Piggy Pig
$test->save (' new.xml '); Save As new file
http://www.bkjia.com/PHPjc/326709.html www.bkjia.com true http://www.bkjia.com/PHPjc/326709.html techarticle in the recent reading of PHP teaching video, which referred to the PHP operation of XML documents, learned a little DOMDocument class. I look at the manual and all English, do not understand. But still wrote a class, realize ...