the php simplexml is a php extension that allows users to easily manipulate xml data. it was produce in php 5 as an object oriented approach to the xml dom providing an object that can be processed with normal property selectors and array iterators.

why use simpleXML

using it, you can handle documents with any degree of complexity.

you don't need the dom extension

simplexml gives two functions that use to import xml data into a simplexmlelement object:

simplexml_load_file( )loads the xml file with name filename as a simplexmlelement object
simplexml_load_string( )loads the supplies xml string as a simplexmlelement object

the php simplexml use this demo.xml file in below example:

     <?xml    version="1.0"    encoding= "utf-8" ?>
               <item  type = "fruit">

reading an XML Document

the php simplexml_load_file() and simplexml_load_string() function read the entire dom tree into memory and returns a simplexmlelement object representation of it.

  <html>  <head> <title> read xml document using simplexml </title>

          <?php         // first  load  data.xml file 
             $output   =  simplexml_load_file( "stocklist.xml", "simplexmlelement");

                   // print_r() function display the object details 

look the code output:

SimpleXMLElement Object ( [item] => simpleXMLElement Object ( [@attributes] => Array 

( [type] => fruit ) [name] => apple [price] => 12.12 [quantity] => 220 ) )

we can access direct to particular element such as:

<html>  <head> <title> read xml document using simplexml </title>

          <?php         // first  load  data.xml file 
             $output   =  simplexml_load_file( "stocklist.xml", "simplexmlelement" );

             echo     'name : '. $output->item[0]->name;   echo "<br/>";
             echo     'price : '. $output->item[0]->price; echo "<br/>";
             echo     'quantity : '. $output->item[0]->quantity."<br/>";   
            //iterate SimpleXMLElement object using foreach loop 
            foreach   (  $output as $item){
                 echo  'name : '. $item->name;  echo  "<br/>";
                 echo  'price : ' . $item->price;

the output of the code above will be:

name : apple
price : 12.12
quantity: 220
name : apple
price : 12.12

creating an XML Document using SimpleXML

php simplexml perfectly possible to create an xml document with simplexml.


             // create the root  'stocklist' element
             $stocklist  =  new   simplexmlelement( "<stocklist/>");
             // create the first 'item' element
             $item  =  $stocklist->addchild( "item" );
             $item->addattribute( "type", "fruit" );
             // create the item's name child element
             $item->addchild( "name", "apple" );
             // create the item's  price child  element
             $item->addchild( "price", "34556562" );
             // create the item's quantity child element
             $item->addchild( "quantity", "343");
             // output the xml  document, encoding markup characters as needed
             echo  htmlspecialchars( $stocklist->asxml() );

the output of the code above will be:

<?xml version="1.0"?> <stocklist><item type="fruit"><name>apple</name>