XML and PHP Simplified - XML,PHP and the Database | WebReference

XML and PHP Simplified - XML,PHP and the Database

By Leidago Noabeb


[next]

XML PHP and the Database

In the previous article, we looked at how to use XML with PHP and saw that it was not very different from writing normal PHP procedures to interact with an XML document. Because PHP treats an XML document like a normal file, it is easy to interact with it, even though its structure is complicated. For example to create a XML document, we use the common functions that PHP uses to create files. For example:

The above code demonstrates an easy way to create an XML document. The problem with the above code is that the data in it is static. What if we have a site that needs to share updated content with other programs or applications or websites? Most sites that have this kind of dynamic content are link to a database that provides this kind of information. Let's say, for arguments sake, that you want to make a list of contacts available to any and all applications. These contact names are stored in a database. What you will have to do is first create some kind of data input form that will collect the information and store it in a database. Because we want different applications to be able to process the information, we create an XML document and put it up on our website. For anyone who wants to view a list of our contacts we create a link to the XML document. This creates a link between the database and the XML document. So how do we achieve this? Let's take a look at a practical example.

Our document is going to be called contacts.xml; it will be updated via our database also called contacts, which contains a table called contact. First, we create the database table. We want the table to have the following fields:

  • name – name of the contact
  • email – email address of the contact
  • address- address of contact

Feel free to add or remove as much information as you want. Below is the SQL to create the database table and some sample data:

Simply copy and paste the data into your MYSQL client. Finally, we create the XML document. The document will have the following structure:

Now for the actual program that will create the XML document. This program will use live data that is stored in the database to create an XML file and fill in all the information that is needed. First, it starts with a page that is going to present a list of options to choose from. The program has four scripts in total:

menu.php:

inputform.php – This script enables the user to input up to date contacts data. Below is the code for it:

The HTML portion of the script has the following code:

The form.php script simply enables the user to create an xml document; we have already looked at this script. Here's its code:

The HTML part of the script has the following code:

The last script is called write2toxmldoc.php. It extracts the latest contact details from the database and creates an updated xml document. It also provides a link to the location of this file. Below is its code:

It has the following HTML code:

The above program works very well when creating and writing to an XML document, but it will become very difficult when you have to read XML data from an xml document. Why? Because an XML document is very complicated, they are not as straight forward as the normal files. The XML documents that we used so far are very simple and straightforward; a more complicated XML document cannot realistically be accessed by the file functions that we have used so far. PHP has specific functions that can access any part of an XML document and retrieve that information. We will look at some of those functions in a bit. In the next section, we will discuss the code and then look at how to format the XML document so that it can be viewed in a web browser.


[next]