PHP SimpleXML method Read and write XML interface file instance parse _php skill

Source: Internet
Author: User
Tags addchild

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-&GT;ITEM[1]-&GT;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

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.