It is convenient to read and write XML documents in PHP5, and you can quickly parse and generate XML-formatted files directly using the PHP SimpleXML method, as illustrated below:
There are three ways to create a SimpleXML object:
1. Create with new keyword
Copy Code code as follows:
$xml = "<personinfo><item><id>1</id><name>aaa</name><age>16</age ></item>
<item><id>2</id><name>bbb</name><age>26</age></item></ Personinfo> ";
$rss =new simplexmlelement ($xml);
2. use Simplexml_load_string () to create
Copy Code code as follows:
$xml = "<personinfo><item><id>1</id><name>aaa</name><age>16</age ></item>
<item><id>2</id><name>bbb</name><age>26</age></item></ Personinfo> ";
$rss =simplexml_load_string ($xml);
3. use Simplexml_load_file () to create from a URL
Copy Code code as follows:
$rss =simplexml_load_file ("Rss.xml");
Or:
$rss =simplexml_load_file ("/rss.xml");//Remote document
Specific examples are as follows:
Copy Code code as follows:
<?php
$xml = "<personinfo><item><id>1</id><name>aaa</name><age>16</age ></item><item><id>2</id><name>bbb</name><age>26</age></ Item></personinfo> ";
$rss =new simplexmlelement ($xml);
foreach ($rss->item as $v) {
echo $v->name, ' <br/> ';
}
echo $rss->item[1]->age;//Read data
Echo ' $rss->item[1]->name= ' CCC ';//Modify Data
foreach ($rss->item as $v) {
echo $v->name, ' <br/> ';//aaa <br/> CCC <br/>
}
Echo ' Unset ($rss->item[1])//Output data
foreach ($rss->item as $k => $v) {
echo $v->name, ' <br/> ';//aaa <br/>
}
Echo ' Add data
$item = $rss->addchild (' item ');
$item->addchild (' id ', ' 3 ');
$item->addchild (' name ', ' ccc_new ');
$item->addchild (' age ', ' 40 ');
foreach ($rss->item as $k => $v) {
echo $v->name, ' <br/> ';//aaa <br/> ccc_new <br/>
}
$rss->asxml (' personinfo.xml ');
?>
Further analysis of the above examples is as follows:
Copy Code code as follows:
reading of XML data
You can access specific elements directly from the name of the element. All elements in a document are considered to be attributes of that object.
foreach ($rss->item as $v) {
echo $v->name, ' <br/> ';//aaa <br/> bbb <br/>
}
Echo $rss->ITEM[1]->AGE;//26
XML data modification, you can directly edit the content of an element directly by using the method of assigning value to the object attribute.
$rss->item[1]->name= ' CCC ';//Modify Data
foreach ($rss->item as $v) {
echo $v->name, ' <br/> ';//aaa <br/> CCC <br/>
}
You can use the PHP content function unset to remove an element from the tree
Unset ($rss->item[1]);
foreach ($rss->item as $v) {
echo $v->name, ' <br/> ';//a www.jb51.net aa <br/>
}
XML adds element data and can be implemented by the Addchild method of the object
$item = $rss->addchild (' item ');
$item->addchild (' id ', ' 3 ');
$item->addchild (' name ', ' ccc_new ');
$item->addchild (' age ', ' 40 ');
foreach ($rss->item as $k => $v) {
echo $v->name, ' <br/> ';//aaa <br/> ccc_new <br/>
}
Storage of XML data
Use the Asxml () method of the object
$rss->asxml (' personinfo.xml ');//save XML data to Personinfo.xml file