XML

Exporting an XML Document from a Database

If you need to pull data from a database for processing as XML on a one-time basis, or maybe periodically but not necessarily in real-time, you might consider just exporting the data manually. Most databases offer an "export as XML" option that converts a database table into a structured XML document with the database columns turned into XML tags. This is a very simple approach to quickly generating an XML document from a database that you might now otherwise be able to access without database tools.

I regularly use the MySQL database for online projects. MySQL is a very popular open source database that does a great job for small- to medium-scale applications. A nice front-end is available for MySQL called phpMyAdmin, which provides a web-based user interface for interacting with a MySQL database. phpMyAdmin provides a very easy-to-use export feature that will export any MySQL database as an XML document.

If you're interested in using MySQL and phpMyAdmin, please visit http://www.mysql.com/ and http://www.phpmyadmin.net/. The details of installing and configuring a MySQL database are unfortunately beyond the scope of this lesson but you'll find plenty of information at the previously mentioned web sites.

To get started exporting an XML document from a MySQL database, open the database in phpMyAdmin, and select the table you want to export. Then click the Export tab. Within the Export options, click XML to indicate that XML is the output data format. If you want to generate an XML file that is stored on the web server, click the Save As File option. Otherwise, just click the Go button to generate the XML code and view it directly in the browser. Figure 19.3 shows how the exported XML document is shown in a web browser.

Figure 19.3. The newly exported XML document is immediately opened in the web browser.

Now you can choose to save the XML file locally or otherwise use the XML code for further processing and manipulation. The key point to realize is that with one button click you've converted an entire tabular database into a well-formed XML document.