Custom PHP Class (Find/Modify) XML document _php Tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.