Operating XML document program code in PHP

Source: Internet
Author: User
Tags foreach php script
The code is as follows Copy Code

/*
<?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

The above is using the DOM to operate, we use the simplexml in PHP to manipulate the XML, but also a very standard operation of the XML document class.

Simplexml_load_file (string filename)
The filename variable here is the filename and the path where the XML data file is stored. The following code uses the Simplexml_load_file function to create a SimpleXML object.

The code is as follows Copy Code

<?php
$xml = simplexml_load_file (' Example.xml '); Creating SimpleXML Objects
Print_r ($xml); Output XML
?>

Where the Example.xml store data is exactly the same as the $data above, and the results are exactly the same.
The above two methods implement the same function, the difference is that the data source of the XML is different. If the XML data source is in a php script file, you need to use simplexml_load_string to create it. If the XML data source is in a separate XML file, you need to use simplexml_load_file to create it.

Reading tags in XML data
Like a variable that operates on an array type, reading XML can be done in a similar way. For example, if you need to read the "name" attribute under each "depart" tab in the XML data above, you can do so by using the Foreach function, as shown in the following code
as shown.

The code is as follows Copy Code

<?php $xml = simplexml_load_file (' Example.xml '); foreach ($xml->depart as $a)
{
echo "$a->name <BR>";
}
?>

The results of the operation are shown below.
Production support
Testing Center
Read XML file//loop read every depart tag in XML data
Output the name attribute
You can also use the square brackets "[]" to read directly the label specified in the XML data. The following code prints the "name" attribute of the first "depart" label in the XML data above.

The code is as follows Copy Code
<?php
$xml = simplexml_load_file (' Example.xml '); reading XML files
echo $xml->depart->name[0]; Output node
?>

The results of the operation are shown below.
Production support
For all child tags under a label, the SimpleXML component provides a children method for reading. For example, for the "depart" label in the XML data above, it includes two sub tags: "name" and "Employees". The following code implements the reading of a child label under the first "depart" label.

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.