Zend PHP 5 – Data Formats and Types – SimpleXML

The SimpleXML extension provides a very simple and easily usable toolset to convert XML to an object that can be processed with normal property selectors and array iterators.

Basic SimpleXML usage can be used by either loading xml strings by passing the string in to the SimpleXML class or by using the simplexml_load_string() function. We can also load xml files using the simplexml_load_file() function.

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
    <movie>
        <title>PHP: Behind the Parser</title>
        <plot>
            So, this language. It's like, a programming language. Or is it a
            scripting language? All is revealed in this thrilling horror spoof
            of a documentary.
        </plot>
    </movie>
</movies>
XML;

$movies = new SimpleXMLElement($xmlstr);
print_r($movies);

Outputs:

SimpleXMLElement Object ( [movie] => SimpleXMLElement Object ( [title] => PHP: Behind the Parser [plot] => So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. ) )

Note that you can also use the simplexml_load_string() function to output the same result.

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
    <movie>
        <title>PHP: Behind the Parser</title>
        <plot>
            So, this language. It's like, a programming language. Or is it a
            scripting language? All is revealed in this thrilling horror spoof
            of a documentary.
        </plot>
    </movie>
</movies>
XML;

$movies = simplexml_load_string($xmlstr);
print_r($movies);

We can obtain certain elements of the XML string by using the following notation:

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
    <movie>
        <title>PHP: Behind the Parser</title>
        <plot>
            So, this language. It's like, a programming language. Or is it a
            scripting language? All is revealed in this thrilling horror spoof
            of a documentary.
        </plot>
    </movie>
</movies>
XML;

$movies = simplexml_load_string($xmlstr);
$movies->movie->title; <!-- Outputs: PHP: Behind the Parser -->

Accessing elements within an XML document that contain characters not permitted under PHP’s naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

This is used in the following example:

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
    <movie>
        <title>PHP: Behind the Parser</title>
        <the-plot>
            So, this language. It's like, a programming language. Or is it a
            scripting language? All is revealed in this thrilling horror spoof
            of a documentary.
        </the-plot>
    </movie>
</movies>
XML;

$movies = simplexml_load_string($xmlstr);
$movies->movie->{'the-plot'}; <!-- Outputs: So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary.

SimpleXML supports the use of xpath:

$movies = new SimpleXMLElement($xmlstr);

foreach ($movies->xpath('//movie') as $movie) {
    echo $movie->name;
}

The data in SimpleXML doesn’t have to be constant. We can manipulate elements such as:

$movies->movie[0]->title = 'Some Other Movie';

The SimpleXMLElement class also contains other useful functions that we can explore:

attributes()

The attributes() function identifies an element’s attributes.

<?php
$string = <<<XML
<a>
 <foo name="one" game="lonely">1</foo>
</a>
XML;

$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}

// Outputs:
name="one"
game="lonely"

getName()

The getName() function gets the name of the XML element:

$sxe = new SimpleXMLElement($xmlstr);

echo $sxe->getName() . "\n";

foreach ($sxe->children() as $child)
{
    echo $child->getName() . "\n";
}

// Outputs:
movies
movie

children()

The children() function finds children of given node:

$movies = new SimpleXMLElement($xmlstr);
print_r($movies->children());

// Outputs:
SimpleXMLElement Object ( [movie] => SimpleXMLElement Object ( [title] => PHP: Behind the Parser [characters] => SimpleXMLElement Object ( [character] => Array ( [0] => SimpleXMLElement Object ( [name] => Ms. Coder [actor] => Onlivia Actora ) [1] => SimpleXMLElement Object ( [name] => Mr. Coder [actor] => El ActÓr ) ) ) [plot] => So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. ) )

asXml()

The asXml() function returns a well-formed XML string based on SimpleXML element:

$movies = new SimpleXMLElement($xmlstr);
echo $movies->asXml();

// Outputs:
<?xml version="1.0"?>
<movies>
 <movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El ActÓr</actor>
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
 </movie>
</movies>

xpath()

The xpath() function runs an XPath query on XML data:

$string = <<<XML
<a>
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <d>
  <c>code</c>
 </d>
</a>
XML;

$xml = new SimpleXMLElement($string);

$result = $xml->xpath('/a/b/c');

while(list( , $node) = each($result)) {
    echo '/a/b/c: ',$node,"\n";
}

// Outputs:
/a/b/c: text 
/a/b/c: stuff

Note: This article is based on PHP version 5.5.