SimpleXML on adding and deleting XML

Source: Internet
Author: User
Tags addchild

SimpleXML is a way for PHP to process XML files, and the other is Dom processing, which only says SimpleXML.
At present, PHP processing XML with more, more mature or DOM. But Dom is still more criticized for its speed and the amount of code.

Some of SimpleXML's functions require a higher version of PHP, which is recommended to be php5.2 above.

One, create a new XML

Method 1 (normal new file):

<?php/** * @author blog.anchen8.net * @copyright * */$fp =fopen (' Xmldoc.xml ') , ' W '); Var_dump ($FP);   $xmlContent = ' <?xml version= ' 1.0 "encoding=" Utf-8 "?>";   $xmlContent. = ' <navlist> ';   $xmlContent. = ' <nav id= ' 1 ">";   $xmlContent. = ' <name> me </name> ';   $xmlContent. = ' <color> #000000 </color> ';      $xmlContent. = ' </nav> ';     $xmlContent. = ' <nav id= ' 2 ">";   $xmlContent. = ' <name> you you </name> ';   $xmlContent. = ' <color> #ffffff </color> ';      $xmlContent. = ' </nav> ';   $xmlContent. = ' <nav id= ' 3 ">";   $xmlContent. = ' <name> he he </name> ';   $xmlContent. = ' <color> #cccccc </color> ';      $xmlContent. = ' </nav> ';   $xmlContent. = ' </navlist> ';   Fwrite ($fp, $xmlContent);   Fclose ($FP); ?> 

Method 2 (new with SimpleXML ):

$fp =fopen (111.xml ', ' W '); $xmlContent = ' <?xml version= ' 1.0 "encoding=" Utf-8 "?><navlist></navlist>"; Fwrite ($fp, $xmlContent); Fclose ($FP); $xml =simplexml_load_file (' 111.xml ');  $app = $xml->addchild (' nav '); $app->addattribute (' id ', ' 1 '); $app->addchild (' name ', ' I am me '); $app->addchild (' Color ', ' #000000 ');  $app = $xml->addchild (' nav '); $app->addattribute (' id ', ' 2 '); $app->addchild (' name ', ' You you You '); $app->addchild (' Color ', ' #ffffff ');  $app = $xml->addchild (' nav '); $app->addattribute (' id ', ' 3 '); $app->addchild (' name ', ' he he he '); $app->addchild (' Color ', ' #cccccc ');  $xml->asxml (' 111.xml ');

You can see that the first method is just plain write file, the second method is to use SimpleXML to create a new XML file, but why should I put the first common method in front of it? Because I tested the run time of the two methods and found that the first method used less time, it is recommended to use the normal new file method to create a new XML file on the line!

The new file structure is as follows:

<?xml version= "1.0" encoding= "Utf-8"?><navlist> <nav id= "1" > <name> i-I </nname> < color> #000000/ncolor> </nav> <nav id= "2" > <name> you you </name> <color> #ffffff </ color> </nav> <nav id= "3" > <name> He he </name> <color> #cccccc </color> </nav ></navlist>

two, reading XML

  if ($xml =simplexml_load_file (' 111.xml ')) {          foreach ($xml->nav as $list) {                       $value []=get_object_vars ($list) ;           }     } else{              Echo (' Load XML error! ');     }

Get_object_vars function is to convert the object into an array, so that $value is a common Php arrays, you can print_r this array, Php array of reading no need I say it.

Third, modify the XML

1 , create a new node

$xml =simplexml_load_file (' 111.xml ');  $app = $xml->addchild (' nav '); $app->addattribute (' id ', ' 4 '); $app->addchild (' name ', ' it it it '); $app->addchild (' Color ', ' #000000 '); $xml->asxml (' 111.xml ');

Append as last node

2 , modify the node

$xml =simplexml_load_file (' 111.xml ');  $XG = $xml->xpath ("/navlist/nav[@id = ' 3 ']");  $xg [0]->name= "He ah he ah"; $xg [0]->color= "#444444"; $xml->asxml (' 111.xml ');

Modified the Nav Property ID 3 node

3 , delete node

Deleting a node with the unset function is a hassle, but deleting the specified node is troublesome, and removing the specified node can only use the node's index value.

For example, delete the second node (ID 2):

$xml =simplexml_load_file (' 111.xml '); Unset ($xml->nav[1]); $xml->asxml (' 111.xml ');

The index starts at 0, so the index of the second node is 1.

If you want to delete through the specified property, you need to loop all nodes to get the node index of the specified property
As follows:

$xml =simplexml_load_file (' 111.xml '); $i = 0; foreach ($xml as $dup) {  $SC = $dup->attributes ();  if ($SC [' id ']==2) {   unset ($xml->nav[$i]);  }  $i + +; } $xml->asxml (' 111.xml ');

Four, delete XML

if (file_exists (' 111.xml ')) {    

Five , the XML document and PHP in the same file situation

<?php$xmldoc = <<<xml<?xml version= "1.0" encoding= "Utf-8"?> <phplamp> <post>       <title id= "1" >php XML processing Introduction </title> <details> details one </details> </post>       <post> <title id= "2" >php XML processing Introduction Two </title> <details> details two </details> </post> <post> <title id= "3" >php XML processing introduction three </title> <details> details three  </details> </post> </phplamp> XML; $movies = new SimpleXMLElement ($xmldoc);       foreach ($movies as $key = + $value) {//Get property $attr = $value->title->attributes (); echo "Id:". $attr [' id '].    "<br/>"; echo "Title:". $value->title.       "<br/>"; echo "Details:". $value->details.   "<br/><br/>";  }//$words = $lib->xpath ("//word"); echo $words [0][' Add ']. ' <br/> ';  Echo $movies->title[0]; ?>

Output Result:

id:1title:php XML Processing Introduction to Details: A detailed description of the id:2title:php XML processing two details: Details two id:3title:php XML processing introduction Three details: detailed content Three

SimpleXML on adding and deleting XML

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.