Zend PHP 7 Certification – Data Formats and Types – SimpleXML

This post covers the SimpleXML section of the Data Formats and Types chapter when studying for the Zend PHP 7 Certification.

The SimpleXML extension provides a very simple and easily usable toolset to convert XML to an object.

Basic SimpleXML usage includes being XML strings by passing the string in to the SimpleXML class or by using the simplexml_load_string() function.

XML files can also be loaded using the simplexml_load_file() function.

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
    <movie>
        <title>Gone with the Wind</title>
        <plot>
            An American epic historical romance film
        </plot>
    </movie>
</movies>
XML;

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

Outputs:

SimpleXMLElement Object ( [movie] => SimpleXMLElement Object ( [title] => Gone with the Wind [plot] => An American epic historical romance film ) )

simplexml_load_string() can also be used to achieve the same result.

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
    <movie>
        <title>Gone with the Wind</title>
        <plot>
            An American epic historical romance film
        </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>Gone with the Wind</title>
        <plot>
            An American epic historical romance film
        </plot>
    </movie>
</movies>
XML;

$movies = simplexml_load_string($xmlstr);
$movies->movie->title; <!-- Outputs: Gone with the Wind -->

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>Gone with the Wind</title>
        <the-plot>
            An American epic historical romance film
        </the-plot>
    </movie>
</movies>
XML;

$movies = simplexml_load_string($xmlstr);
$movies->movie->{'the-plot'}; <!-- Outputs: An American epic historical romance film

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, including attributes(), which 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() 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() function finds children of given node:

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

// Outputs:
SimpleXMLElement Object ( [movie] => SimpleXMLElement Object ( [title] => Gone with the Wind [characters] => SimpleXMLElement Object ( [character] => Array ( [0] => SimpleXMLElement Object ( [actor] => Clark Gable ) [1] => SimpleXMLElement Object ( [actor] => Vivien Leigh ) ) ) [plot] => An American epic historical romance film ) )

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>Gone with the Wind</title>
  <characters>
   <character>
    <actor>Clark Gable</actor>
   </character>
   <character>
    <actor>Vivien Leigh</actor>
   </character>
  </characters>
  <plot>
   An American epic historical romance film of a documentary.
  </plot>
 </movie>
</movies>

xpath() 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

View the other sections:

Note: This article is based on PHP version 7.0.