PHP SimpleXML Parser

In the last tutorial I gave an introduction to XML parsers in PHP and let us recollect key information from that. SimpleXML parser,

  • requires libxml extension
  • is an easy and simple to use tool for XML parsing
  • requires PHP 5 or above
  • is tree-based parser
  • is interoperable with DOM parsers and can parse DOM formatted XML documents.

simple_tree_structure

Apart from the above, PHP SimpleXML parser is capable of working via both procedural and object oriented style to parse XML document.

SimpleXML Functions

1. simplexml_load_file()

  • Syntax:
    simplexml_load_file(($fileName,$class,$options,$ns,$is_prefix)
  • This function accepts XML file path as its first parameter and it is mandatory.
  • It converts XML into SimpleXMLElement Object by default. Otherwise, we can use our custom class ($class parameter) which should extend SimpleXMLElement.
  • We can set additional libxml parameters using $options.
  • $ns is for specifying namespace prefix. If it is set, then $is_prefix flag will be TRUE.

2. simplexml_load_string()

  • Syntax:
    simplexml_load_string($XMLData,$class,$options,$ns,$is_prefix)
  • This function differs from the above one by accepting XML document, instead of file reference. Other parameters are same as simplexml_load_file().

3. simplexml_import_dom()

  • Syntax:
    simplexml_load_string($DOMNode,$class)
  • This function accepts DOM formatted XML content as its argument and converts it into SimpleXMLElement object by default.

SimpleXML Classes

SimpleXML parser extension includes classes,

  1. SimpleXMLElement
  2. SimpleXMLIterator

Both of the above classes includes several properties to access XML nodes while parsing. For example, we can

  • get children nodes of specified root
  • add new child/attribute
  • get name of a particular element
  • get current/next element while parsing

SimpleXMLIterator inherits SimpleXMLElement  class to implement a RecursiveIterator to perform recursive parsing on XML nodes.

PHP SimpleXML Parser Example

Now, its time to experiment the above SimpleXML functions to parse a XML document. First, we should create XML document. Let us take a simple XML document,

<?xml version="1.0"?>
<toys>
<toy>
<name>Ben 10 Watch</name>
<type>Battery Toys</type>
</toy>
<toy>
<name>Angry Birds Gun</name>
<type>Mechanical Toys</type>
</toy>
</toys>

We can directly load this XML using simplexml_load_string(). Otherwise, we can save this file as toys.xml and send it for simplexml_load_file()

<?php
$xml = simplexml_load_file("../toys.xml");
print "<PRE>";
print_r($xml);
print "</PRE>";
?>

As a result, SimpleXMLElement object will be returned as,

SimpleXMLElement Object(
[toy] => Array(
[0] => SimpleXMLElement Object(
[name] => Ben 10 Watch
[type] => Battery Toys
)
[1] => SimpleXMLElement Object(
[name] => Angry Birds Gun
[type] => Mechanical Toys
)
)
)

Adding Child Element using SimpleXML Parser

To add a child element to a node in a XML document, we use addChild() of SimpleXMLElement class.

<?php
$xmlDocument = '<?xml version="1.0"?>
<toys xmlns:h="http://www.w3.org/TR/html4/">
<toy>
<name>Ben 10 Watch</name>
<type>Battery Toys</type>
</toy>
<toy>
<name>Angry Birds Gun</name>
<type>Mechanical Toys</type>
</toy>
</toys>';
$xml = new SimpleXMLElement($xmlDocument);
$toy = $xml->addChild('toy');
$toy->addChild('name', 'Remote Control Car');
$toy->addChild('type', 'Remote Control Toys');
print "<PRE>";
print_r($xml);
print "</PRE>";
?>

Download PHP SimpleXML Parser Source Code

This PHP code tutorial was published on November 15, 2013.

↑ Back to Top