PHP SimpleXML Parser

by Vincy. Last modified on July 14th, 2022.

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 a tree-based parser
  • is interoperable with DOM parsers and can parse DOM formatted XML documents.

Apart from the above, the PHP SimpleXML parser is capable of working via both procedural and object-oriented styles to parse an 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 the namespace prefix. If it is set, then the $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 documents, instead of file references. 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 a SimpleXMLElement object by default.

SimpleXML Classes

SimpleXML parser extension includes classes,

  1. SimpleXMLElement
  2. SimpleXMLIterator

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

  • get children nodes of the specified root
  • add new child/attribute
  • get the 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, it’s time to experiment with the above SimpleXML functions to parse an XML document. First, we should create an 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, the 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

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page