This is the first article in a series of tutorials that will guide you through the basics of using XML for data storage in a PHP application. I will be walking you through creating a php xml application that and in the same pulse teach you how to manipulate XML documents in a PHP application that stores information in XML format.
Now you might be wondering why you would want to have your information stored in an XML file instead of a traditional database. Well there are a few reasons you might want to use XML instead of a MySQL database. One reason might be that you do not have a lot of information to store and would much rather use simple file based storage while having some human readable structure that you can follow.You may have a hosting account that doesn’t include a database and would find it simpler using existing assets.
It must be noted though that generally XML was intended as a data-exchange format and not as a storage format.
First let me give you a brief introduction to the technologies used in this application.
XML stands for Extensible Markup Language and is a markup language that allows you to define your own tags, hence the extensible part. XML was designed to transport and store data.
I will be using PHP as my server side scripting language and I will also use SimpleXML, which is a PHP extension that was introduced in PHP5 as a simple way to get XML element attributes and text.
Although SimpleXML simplifies access to XML elements and attributes, it is rather limited in its functionality so I will also use XML DOM to manipulate my XML data. XML DOM or Document Object Model uses a tree based parser to read, create and update an XML document.
First things first, we would like to be able to read an XML document and display its information in a table list. I will be creating an application that stores banner information for companies that want to advertise their products on my website. I would like to be able to store the company’s name, the banner image and maybe the company link to their products page.
From this simple description I already have a structure for my XML document.
<?xml version="1.0" encoding="UTF-8"?> <banners> <banner> <bannerid>1</bannerid> <bannerimage>banner_1.jpg</ bannerimage > <company>ABC Technologies</company> <website>www.abctechnologies.com</website> </banner> <banner> <bannerid>2</bannerid> <bannerimage>banner_2.jpg</ bannerimage > <company>DEF Technologies</company> <website>www.def-incorpoprated.com</website> </banner> <banner> <bannerid>3</bannerid> <bannerimage>banner_3.jpg</ bannerimage > <company>GHI Design</company> <website>www.ghidesign.com</website> </banner> </banners>
Reading this XML file with SimpleXML very easy using the simple_xml_load_file function is as shown in the code listing below. This creates an object out of our XML file that can be accessed as a normal PHP object. Next comes the foreach loop that loops through the objects child elements and displays each elements text node value.
<table border="1"> <tr> <td><a href="banner_edit.php?banner_id=0">Add Banner</a></td> <td> </td> <td> </td> <td> </td> <td> </td> </tr> <tr> <td>Banner Id</td> <td>Company</td> <td>Website</td> <td colspan="2">Actions</td> </tr> <?php $xml = simplexml_load_file('banners.xml') or die("Error: Cannot create object"); ?> <?php foreach ($xml->children() as $banner => $data) : ?> <tr> <td><?php echo $data->bannerid; ?></td> <td><?php echo $data->company; ?></td> <td><?php echo $data->profile; ?></td> <td><a href="banner_edit.php?banner_id=<?php echo $data->bannerid; ?>">Edit</a></td> <td><a href="banner_delete.php?banner_id=<?php echo $data->bannerid; ?>">Delete</a></td> </tr> <?php endforeach; ?> </table>
In the last few lines I also added links to banner_edit.php and banner_delete.php and passed the bannered as a parameter. These pages will be used for the update and delete parts of the application. At the top of the table I also added an “Add banner” link that will also go to edit_banner.php with the banner_id parameter set to 0 representing a new record.
And that’s it for now. In this tutorial we have learnt how to access XML elements using SimpeXML and displaying them as a index table with links to edit and delete tables.
In future tutorials I will walk through simple “edit” and “delete” pages that update and delete selected XML elements from our document.