#native_company# #native_desc#
#native_cta#

Dynamic XML with PHP

By Ben Robinson
on July 10, 2008

In this article I will show how to generate dynamic XML documents with Object-Oriented PHP. Before I get started, let’s get right to the heart of this functionality.
Although the code may look fancy, being packaged up inside objects and inheritance, the key elements of this functionality rest in the the mysql_list_fields() and mysql_num_fields() functions.
What they do is get a field list array and a column count for a given table in a database. The way that we’ll be able to have a generic XML output method in our class is by taking advantage of these features. So, in simplicity, instead of outputting our XML by handcoded column names, we are going to create a method that dynamically creates XML output with only a record id and the table name to query from. The implementation, however, is a bit more complex.
Let’s start with our output XML method and work backwards from there:
function gen_xml($rs) {
		
		//clear XML_out values
$this->XML_out = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
		$this->XML_out .= "<response>";
		do {
		foreach ($this->fieldnames as $value) {
		$this->XML_out .= "<".$value.">".$rs[$value]."</".$value.">";
			}
		} while ($this->rs = $this->fetch_array($this->result));	
		
		$this->XML_out .= "</response>";
	}
Now we’re going to break this down piece by piece. First, let’s take a step back and look at how we got our recordset:
function get_item($ID, $tablename) { 

		$this->set_fields($tablename);
		$query = "SELECT * FROM ".$tablename." WHERE ID = ".$ID;
		$this->result = $this->query($query, $this->dblink) 
or die($this->return_error($query, mysql_error()));
		$this->rs = $this->fetch_assoc($this->result);	
//set your current output XML text using the current recordset and fieldnames
		$this->gen_xml($this->rs);	
		echo $this->XML_out;
	}
In this case I have a method called get_item, which takes a key value and the tablename.
I have a method inside my database class that will set the current internal array fieldnames to the ones associated with the table you are querying, called set_fields (this method is in the MysqlDB classfile in the code example):
function set_fields($tablename) {
        	
        	$this->fields = mysql_list_fields($this->db, $tablename, $this->link);
			$this->columns = mysql_num_fields($this->fields);
			
			for ($i = 0; $i columns; $i++) {
				
    		$this->fieldnames[$i] = mysql_field_name($this->fields, $i);
    		
			}
        }
Every time this method is called, it creates a new internal array I can use to generate the XML from my query. Let’s go back to that first method. Every time the method is run, we clear our XML_out variable and initialize the XML headers:
$this->XML_out = "";
$this->XML_out .= '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
		$this->XML_out .= "<response>";

First we start out with a do while loop which will iterate through our current recordset:

Do { //xml code will go here } while ($this->rs = $this->fetch_array($this->result));	

Then, the core functionality of the method is in these lines: 

foreach ($this->fieldnames as $value) {
	$this->XML_out .= "<".$value.">".$rs[$value]."</".$value.">";
}
This will simply concatenate the XML_out variable with each column value that is relevant to each item in the recordset, and for each column name in the fieldnames array.
This is the php side of things, but what if you were using Ajax to parse your XML? Without using any libraries to help you, JavaScript’s inherent XML parsing can be very cumbersome. You can, however, utilize the power of arrays once again to alleviate some of this pain. Here is an example of a JavaScript function that, while knowing what specific column names you need to parse, will simply prepare your output html for you. Let’s say we have a very simple list of books. First, set the column names like this in your JavaScript file:
var elements = new Array(2);
		elements[0] = "title";
		elements[1] = "isbn";

        parse_xml(elements);