SimpleXML is a SAX XML parser. It represents an XML document as a hierarchical set of objects and properties where each element is an object and each attribute is an object property.

Access an XML element using SimpleXML

<?xml version='1.0'?>
<hotels>
   <hotel stars="5">
       <name>President Wilson</name>
       <city>Geneva</city>
       <country>Switzerland</country>
   </hotel>
</hotels>

Save this file as hotel.xml

// load XML file
$xml = simplexml_load_file('hotel.xml') or die ("Unable to load XML");

// access xml element
echo 'Hotel: ' . $xml->hotel->name;

Note that we did not have to call the tag “hotels” since it is the root.

Accessing multiple XML elements using SimpleXML

<?xml version='1.0'?>
<hotels>
   <hotel stars="5">
       <name>President Wilson</name>
       <city>Geneva</city>
       <country>Switzerland</country>
   </hotel>
   <hotel stars="5">
       <name>Montreux Palace</name>
       <city>Montreux</city>
       <country>Switzerland</country>
   </hotel>
   <hotel stars="3">
       <name>Express by Holiday Inn</name>
       <city>Geneva</city>
       <country>Switzerland</country>
   </hotel>        
</hotels>

Save this file as hotels.xml

// show how to access multiple elements with SimpleXML

// load XML file
$xml = simplexml_load_file('hotels.xml') or die ("Unable to load XML");

// access XML elements
foreach ($xml->hotel as $hotel) {
   // print data
   print '<br>Hotel: ' . $hotel->name;
}

// count elements
print '<br>Total: ' . count($xml->hotel);

The foreach loop iterates over the XML elements. count() function allows users to count any type of element.

Accessing attributes of XML elements SimpleXML stores attributes as key value pairs in an associative array.

// accessing attributes of xml elements

// load XML file
$xml = simplexml_load_file('hotels.xml') or die ("Unable to load XML");

// access XML elements
foreach ($xml->hotel as $hotel) {
   // print data
   print '<br>' . $hotel->name . ' is a ' . $hotel['stars'] . ' star hotel';
}

Altering XML element and attribute values This simple example shows how to change XML element and attribute values:

// alter xml elements and attributes using SimpleXML

// load XML file
$xml = simplexml_load_file('hotels.xml') or die ("Unable to load XML");

// change element value
$xml->hotel[2]->name = 'Crown Plaza';

// change attribute value 
$xml->hotel[2]{'stars'} = 4;

// print new XML string
header('Content-Type: text/html');
echo $xml->asXML();

The asXML() function converts SimpleXML object tree to XML. Following is the output of this program

<?xml version="1.0"?>
<hotels>
   <hotel stars="5">
       <name>President Wilson</name>
       <city>Geneva</city>
       <country>Switzerland</country>
   </hotel>
   <hotel stars="5">
       <name>Montreux Palace</name>
       <city>Montreux</city>
       <country>Switzerland</country>
   </hotel>
   <hotel stars="4">
       <name>Crown Plaza</name>
       <city>Geneva</city>
       <country>Switzerland</country>
   </hotel>        

Note that this program only printed an edited XML string to screen, it did not edit the XML file. To edit the XML file, you need to rewrite the entire file.