Home >Backend Development >PHP Tutorial >PHP's SimpleXML method reads and writes XML interface file instance analysis_PHP tutorial

PHP's SimpleXML method reads and writes XML interface file instance analysis_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:24:41662browse

It is very convenient to read and write xml documents in php5. You can directly use php's SimpleXML method to quickly parse and generate xml format files. Here is an example:

There are three ways to create a SimpleXML object:

1.Use new keyword to create

Copy code The code is as follows:

$xml="1< /id>aaa16
2bbb26
";
$rss=new SimpleXMLElement($xml);

2.Use simplexml_load_string() to create

Copy code The code is as follows:

$xml="1< /id>aaa16
2bbb26
";
$rss=simplexml_load_string($xml);

3.Use simplexml_load_file() to create

from a URL

Copy code The code is as follows:

$rss=simplexml_load_file("rss.xml");
//or :
$rss=simplexml_load_file("/rss.xml");//Remote document

Specific examples are as follows:

Copy code The code is as follows:

$xml="1aaa162 bbb26";
$rss=new SimpleXMLElement($xml);
foreach($rss->item as $v){
echo $v->name,'
';
}
echo $rss->item[1]->age;//read Get data
echo '
';
$rss->item[1]->name='ccc';//Modify data
foreach($rss->item as $v){
echo $v->name,'
';//aaa
ccc

}
echo '< ;hr>';
unset($rss->item[1]);//Output data
foreach($rss->item as $k=>$v){
echo $v->name,'
';//aaa

}
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,'
';//aaa
ccc_new

}
$rss->asXML('personinfo.xml');
?>

Further analysis of the above example is as follows:

Copy code The code is as follows:

//Reading xml data
//You can access specific elements directly through the name of the element. All elements in the document are considered properties of the object.
foreach($rss->item as $v){
echo $v->name,'
';//aaa
bbb

}
echo $rss->item[1]->age;//26
//xml data modification, you can directly use the object attribute assignment method to directly edit an element Content
$rss->item[1]->name='ccc';//Modify data
foreach($rss->item as $v){
echo $v-> ;name,'
';//aaa
ccc

}
//You can use the PHP content function unset to remove an element from the tree Delete
unset($rss->item[1]);
foreach($rss->item as $v){
echo $v->name,'

}
//xml adds element data, which can be achieved through 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
ccc_new

}
//Storage of xml data
//Use the asXML() method of the object
$rss->asXML('personinfo.xml');//Store xml data into personinfo.xml file

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825362.htmlTechArticleIt is very convenient to read and write xml documents in php5. You can directly use php's SimpleXML method to quickly parse and generate xml format file, the following example illustrates: Creating a SimpleXML object has...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn