I recently had the need to display an XML configuration file directly in the browser window for purposes of quickly identifying issues with a particular application. I quickly realized that the XML code that is exported on a nightly basis all came out in one long line before being stored in a blob field in a mySQL database.

This topic will help to explain how I was able to reformat the output to be more readable in the browser. Along with some help from a custom class found on PHPClasses.Org, I was able to accomplish this seemingly simple, yet overly complicated task.

First, download Beauty XML from PHPClasses.Org and extract the files to a common library folder on your site.

Next, include the file in the script that will output the XML code.

include_once(FULL_PATH . "libs/beauty_xml.php");

Create an instance of the Beauty XML object:

$bx = new beauty_xml();

Then, simply pass the XML data in a string format to the format_xml function that is supplied with Beauty XML:

echo $bx->format_xml($file);

In this case, I've passed in a string that was queried from my database. The entire file contents were stored in a BLOB field. A simple query pulled the data into a string that could be used to format the data.

Here is a sample of the query that I created using the PEAR DB modules for PHP:

$file = $db->getOne("SELECT configfile from ! where configid = !", array(FIMCONFIG_TABLE, $_GET['configid']));

The end result was a pretty looking XML output file formatted properly with the right number of indents at the right locations. You can see that with a little bit of help from the development community, it is relatively simple to accomplish a complex task with very little effort.