Feb
24
2010

 

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.)

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

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.

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

The output is below:

Example 3

Accessing an attribute.

[c]
<?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;
?>
[/c]

The output is below:

Example 4

Iterating attributes.

[c]
<?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;
}
?>
[/c]

The output is below:

Example 5

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

[c]
<?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");
?>
[/c]

The code above creates an XML that looks like this:

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

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

[c]
<?php
echo $xmlData->book1->attributes()->title. "\n";
echo $xmlData->book2->attributes()->title. "\n";
echo $xmlData->book1->author. "\n";
echo $xmlData->book2->author. "\n";
?>
[/c]

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.

[c]
<?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";
?>
[/c]

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/

One Response to “Parsing XML in PHP Using SimpleXML”

  1. [...] For more information please visist http://www.permadi.com/blog/2010/02/parsing-xml-in-php-using-simplexml/ [...]