PHP 5 Tutorial - Parsing XML in PHP 5 using SimpleXML

PHP5 has come up lot of improvement when it comes to XML parsing as compared to parsing XML in PHP4. PHP5 has come up with a new function called SimpleXML for parsing XML files. This article will guide the PHP programmers on how to use the SimpleXML function in PHP5.





Advantages of SimpleXML

Limitation of SimpleXML

Loading XML Parser in PHP5
In PHP5 there are two ways you can load the XML Parser.

Procedural Way:
In this technique the PHP5 uses SimpleXMLElement class. Before going deep into SimpleXMLElement class PHP5 has 2 functions that returns you the object of SimpleXMLElement class viz simplexml_load_string() and simplexml_load_file().

simplexml_load_string()
This method can only load the xml content stored in a string variable.
Syntax:

< ?    
     simplexml_load_string(xml string content);
?>

simplexml_load_file
This method can only load the external xml files.
Syntax:

< ?   
    // Load an XML file
    $library = simplexml_load_file(xml file);
?>

Object Oriented Programming Way:
If you are already implementing OOPS and you want to parse the xml file using OOPS logic then you can instantiate the SimpleXMLElement class.
Example 1

< ?
	// Load an XML string
	$xmlstr = file_get_contents(xml file); //Storing the xml file content to $xmlstr
	$library = new SimpleXMLElement($xmlstr); //$library now holds the object of SimpleXMLElement
?>

Example 2

< ?
	// Load an XML file
	$library = new SimpleXMLElement(xml file, NULL, true);
?>

The first argument is self explanatory about the xml file to load. Leave the second argument as it is. The third argument indicates whether you are loading string data or external xml file. True indicates loading external xml file and false indicates loading string xml content.

XML Content used by examples in this article:

< ?xml version="1.0" encoding="UTF-8" ?> 
<annual -details>
	<company>
		<turnover>
			<year id="2000">100,000</year>
			<year id="2001">140,000</year>
			<year id="2002">200,000</year>
		</turnover>
		<employee id="001" >Discovery</employee>
	</company>
	<company>
		<employee id="002" >NGC</employee>
		<turnover>
			<year id="3000">200,000</year>
			<year id="3001">240,000</year>
			<year id="3002">300,000</year>
		</turnover>
	</company>
</annual>

Reading XML Tags Text:
PHP5 uses foreach loop to iterate to the xml content.

Example 1:

1
2
3
4
5
6
7
< ?
      $employee_detail = new SimpleXMLElement("employee.xml", NULL, true);
      foreach ($employee_detail->company as $empdetail)
      {
          echo "<br />".$empdetail->turnover->year[0];          
      }
?>

Explanation:
Line2 loads the external xml document employee.xml. Refer the xml content above the example.
Line3 will iterates to the xml document, here i am pointing to the first child element(company) of the root element(annual-details).
Line4: This statement will echo the first year inside every turnover tag found.
$empdetail - Point to every instance of company tag
$empdetail->turnover - Point to the each instance of turnover tag inside the company tag
$empdetail->turnover->year[0] - Point to the each instance of first occurance of year tag inside the tunrover tag of the company tag.

Output of Example1:
100,000
200,000
Accessing Children and Attributes:
On similar lines for reading the XML element text content; reading attributes also uses foreach loop construct. For accessing attributes SimpleXML creates an Associative arrays of all the attributes there are inside the xml content.

Example - Reading Attributes

1
2
3
4
5
6
7
8
< ?
      $employee_detail = new SimpleXMLElement("employee.xml", NULL, true);
      foreach ($employee_detail->company as $empdetail)
      {
          echo $turnover->turnover->year['id']; //Here i am reading the attribute id of the year tag
 
      }	
?>

The major drawback of using this approach is that the it is necessary to know the names of
the elements and attributes getting used in the XML document. So will need to take extreme precaution if there is any change in the structure of the XML document.

But SimpleXML provides a means to access children and attributes without the need to know their names.
SimpleXMLElement object has three methods
SimpleXMLElement::children()
SimpleXMLElement::attributes()
SimpleXMLElement::getName()

SimpleXMLElement::children()
Will return the list of child nodes the XML node has. This method will work along with foreach loop.
Syntax:

< ?
    foreach(xml_object->children() as $variablename)
    {
 
    }
?>

SimpleXMLElement::attributes()
Will return the list of attributes the XML node has. This method will work along with foreach loop.
Syntax:

< ?
foreach(xml_object->current_pointing_node->attributes() as $attributename => $attributevalue)
{
 
}
?>

SimpleXMLElement::getName()
Will return the name of the xml tag.
Syntax:

< ?
       xml_object->current_pointing_node->getName();
?>

Similar Posts

Custom Search

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

I wish I had found this a couple of days ago! I’m using it with Google Code Search API - I finally got it working by myself, but great tutorial.

You can see mine in action at http://pastemonkey.org at the side by selecting search with Google Code.

Leave a comment

(required)

(required)