General
Parsing XML in PHP Using SimpleXML
February 24, 2010
1

There are several method to parse XML data using PHP, one of them is using SimpleXML.
SimpeXML requires PHP 5 or better but is one of the easiest way to parse XML in PHP, you can use object notation to refer to nodes.
The SimpleXML API documentation is here: http://us3.php.net/manual/en/book.simplexml.php

Example 1

Here we show a very basic parsing, using xml text, passed using simplexml_load_string. Then we output the object returned by simplexml_load_string, shown below.  (To load from a file, there is also a simplexml_load_file function, an example of simplexml_load_file is linked at the end of this tutorial.)

<?php
$xmlData=simplexml_load_string("<root><node>This is a node</node></root>");
echo var_dump($xmlData);
?>

The output is below, as you can see, an object containing the XML tree has been created.

Example 2

Here we show how to access the element within the object returned by simplexml_load_string. It’s just the basic object notations.

<?php
$xmlData=simplexml_load_string("<root><node>This is a node</node></root>");
echo $xmlData->node;
?>

The output is below:

Example 3

Accessing an attribute.

<?php
$xmlData=simplexml_load_string("<root><nodes><node1 exampleAttribute='sample attribute'>Node 1 Content</node1></nodes></root>");
echo "Node 1 content is ".$xmlData->nodes->node1;
echo "Node 1 attribute is ".$xmlData->nodes->node1->attributes()->exampleAttribute;
?>

The output is below:

Example 4

Iterating attributes.

<?php
$xmlData=simplexml_load_string("<root><node1 att1='ATT1' att2='ATT2'>Content</node1></root>");
foreach ($xmlData->node1->attributes() as $attributeName=>$attributeValue)
{
 echo "Node 1 has ".$attributeName . " with the value of " . $attributeValue;
}
?>

The output is below:

Example 5

Creating XML: you add child nodes and attributes to the xml object, using addChild and addAttributes.

<?php
$xmlData=simplexml_load_string("<xml></xml>");

// Adding chld nodes
$book1=$xmlData->addChild("book1");
$book2=$xmlData->addChild("book2");

// Adding attributes
$book1->addAttribute("title", "Book1Title");
$book2->addAttribute("title", "Book2Title");

$book1->addChild("author", "Jim");
$book2->addChild("author", "James");
?>

The code above creates an XML that looks like this:

<xml>
 <book1 title="Book1Title">
 <author>Jim</auhor>
 </book1>
 <book2 title="Book2Title">
 <author>James</author>
 </book2>
</xml>

Let’s access some of the nodes and print them out using the code below:

<?php
echo $xmlData->book1->attributes()->title. "n";
echo $xmlData->book2->attributes()->title. "n";
echo $xmlData->book1->author. "n";
echo $xmlData->book2->author. "n";
?>

Here’s the output:

Example 6

Creating XML This is using the same technique as example 5, but using using the SimpleXMLElement constructor.
The constructor takes either a string or URL. Reference: http://us3.php.net/manual/en/simplexmlelement.construct.php.

<?php
$xmlData=new SimpleXMLElement("<xml></xml>");

// Adding child nodes
$book1=$xmlData->addChild("book1");
$book1->addAttribute("title", "Book1Title");
$book1->addChild("author", "Jim");

echo $book1->attributes()->title. "n";
echo $book1->author. "n";
?>

The output is as expected and shown below:

Head over to the next example showing how to use SimpleXML to parse RSS Feed:http://permadi.com/blog/2010/03/integratingreading-rss-feed-using-php/