Php Array To Xml

How to convert any MySQL table and data to a well-formed XML document using PHP. Building a generic solution to a common problem.

In this tutorial we will create a standardized script that will convert a MySQL table of any shape or size into a valid XML document. This can be useful for any situation where you have to transfer data from a MySQL database into an XML based system. In some cases the “mysqldump –xml” might be sufficient. If you require output that’s different from the mysqldump format, or if you don’t have access to the mysqldump tool you will need to code your own solution. Creating a single-use script to handle specific data is of course quite easy, but would require custom coding for each table that needs to be converted to XML. I’m going to show you how (using a handy PHP function) you can build a more generalized solution. This mechanism will allow you to convert a MySQL table to XML, but also to a PHP array, CSV file (even though MySQL already supports CSV natively) or any other required format.

Php Array To Xml Array_walk_recursive

PHP converts an array to XML PHP can convert the array into xml format, the simple way is to traverse the array, and then the array of key / value into xml node, and then directly echo output, such as. Step1: Write XML Data File from Associative Array First we will create a function createXMLFile to implement functionality to create XML from associative array. We will use PHP library class DOMDocument to create XML data and save into XML file. We will create directory XMLFILE in project folder to store XML file. Convert PHP array to XML with support for attributes and CDATA. Entire project is based on this issue responses. Sample usage (it's just a code from my project). The createXML function converts PHP multidimensional array to XML file. The data array needs to be passed as a parameter in createXML function. The createXML function create an XML document using DOMDocument class and insert the PHP array content in this XML document. While converting this array to an XML document, I created a root node called items. Then, I iterated the input array and created child item with the title, link and description nodes on each iteration. PHP Code to Convert Array to XML. This code has the input array structure supplied to the PHP foreach loop to iterate the inner array elements.

Let’s start with an example table and some data:

After running our PHP script the resulting XML will look like this:

<fruits>
<fruit_id>1</fruit_id>
<fruit_colour>yellow</fruit_colour>
</fruit>
<fruit_id>2</fruit_id>
Php array to xml string
<fruit_colour>orange</fruit_colour>

Php Parse Xml

Xml
</fruit>
<fruit_id>3</fruit_id>
<fruit_colour>red</fruit_colour>
</fruit>

Pretty neat huh? So let’s get down to business and start by setting up our database connection:

//database configuration
$config['mysql_user'] = 'root';
$config['db_name'] = 'fruit_store';
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
@mysql_select_db($config['db_name']) ordie( 'Unable to select database');

We start building the actual XML by creating the XML header and the necessary root element (which we derive from the table name, add “s” for plural!):

$xml= '<?xml version='1.0' encoding='UTF-8'?>';
$root_element= $config['table_name'].'s'; //fruits
Xml

And now for the actual fun stuff! We select the items from the table and use the mysql_fetch_assoc() function to turn the query result as an associative array.

$sql= 'SELECT * FROM '.$config['table_name'];
if(!$result= mysql_query($sql))
{
{
foreach($result_arrayas$key=> $value)
//$key holds the table column name
//embed the SQL data in a CDATA element to avoid XML entity issues
$xml.= '</$key>';
}

That’s it! We have created a list of XML elements representing the MySQL data. Now all we have to do is properly close the XML and output the data to (in this case) the browser:

$xml.= '</$root_element>';
//send the xml header to the browser
echo$xml;

The script can be easily modified to allow for different types of XML (think element names vs attributes). If the script is accessed through a URL we can make the script even more flexible by passing the table or column name(s) as a parameter. Always make sure that the SQL data doesn’t hold any (unicode) characters that are not allowed in XML! I will post a few more conversion examples in another post.