Using PHP and SimpleXML to parse XML

Create a breakfast menu from data in XML format with PHP and simpleXML

CertosinoLab
3 min readSep 12, 2021
Photo by Ben Griffiths from unsplash

What is SimpleXML?

As the official documentation says:

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.

SimpleXMLElement is the principal class of this extension, represents an element in an XML document.

The breakfast menu

We will use the following XML document (inspired by this):

<?xml version='1.0' standalone='yes'?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<desc>Two of our famous Belgian Waffles with plenty of real maple syrup</desc>
<calories>650</calories>
<image>images/austin-park-nu0LhZhBMEs-unsplash.jpg</image>
<source>https://unsplash.com/@aussiep</source>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<desc>Light Belgian waffles covered with strawberries and whipped cream</desc>
<calories>900</calories>
<image>images/joyful-vWjvnhkjziI-unsplash.jpg</image>
<source>https://unsplash.com/@joyfulcaptures</source>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<desc>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</desc>
<calories>900</calories>
<image>images/yulia-matvienko-1A7U2FE4eRQ-unsplash.jpg</image>
<source>https://unsplash.com/@yuliamatvienko</source>
</food>
</breakfast_menu>

whereas the menu will look like this:

The Code

We need just two files: restaurant-menu.php and index.php

In restaurant-menu.php we got the xml document, stored in a string ($xmlstr)

<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<desc>Two of our famous Belgian Waffles with plenty of real maple syrup</desc>
<calories>650</calories>
<image>images/austin-park-nu0LhZhBMEs-unsplash.jpg</image>
<source>https://unsplash.com/@aussiep</source>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<desc>Light Belgian waffles covered with strawberries and whipped cream</desc>
<calories>900</calories>
<image>images/joyful-vWjvnhkjziI-unsplash.jpg</image>
<source>https://unsplash.com/@joyfulcaptures</source>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<desc>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</desc>
<calories>900</calories>
<image>images/yulia-matvienko-1A7U2FE4eRQ-unsplash.jpg</image>
<source>https://unsplash.com/@yuliamatvienko</source>
</food>
</breakfast_menu>
XML;
?>

while in index.php we use SimpleXMLElement to make the xml string easily iterable

<?php
include 'restaurant-menu.php';

$restaurantMenu = new SimpleXMLElement($xmlstr);

for($i = 0; $i < count($restaurantMenu->food); $i++) {
echo '
<div style="width: 95vw; display: flex;">
<div style="float: left;">
<img style="border-radius: 20%;" width="120" height="160" src='.$restaurantMenu->food[$i]->image.' />
</div>
<div style="float: right; line-height: 2; font-size: 16px; padding-left: 15px;">
Name: '.$restaurantMenu->food[$i]->name.'<br>
Price: '.$restaurantMenu->food[$i]->price.'<br>
Description: '.$restaurantMenu->food[$i]->desc.'<br>
Calories: '.$restaurantMenu->food[$i]->calories.'<br>
Photo by: '.'<a href='.$restaurantMenu->food[$i]->source.'>'.$restaurantMenu->food[$i]->source.'</a><br>
</div>
</div>
<br>
<br>';
}

basically $restaurantMenu is an object of type SimpleXMLElement which contains within it an array with other objects of type SimpleXMLElement

so we have the XML tree

Get the code

You can find the code of this simple project here: CertosinoLab/mediumarticles at php-simple-xml-1 (github.com)

Thank you for reading!

--

--