creating xml with php

it’s no surprise to me when developers decide to publish data, often times, it’s formatted in xml. it allows for content to be published once and viewed on many different platforms. the widespread adoption and ease of use allow xml to be the de facto standard when it comes to content syndication.

if you aren’t already familiar with xml, the example below illustrates how easily the document can be interpreted by humans and machines.

<?xml version="1.0"?>
<music>
	<song>
		<title>hooch</title>
		<artist>melvins</artist>
		<album>houdini</album>
	</song>
	<song>
		<title>sixtyniner</title>
		<artist>boards of canada</artist>
		<album>twoism</album>
	</song>
</music>

php dom extension

built in support for dom parsing in php makes retrieving data and inserting it into an xml document fairly simple. the dom functions available in php are much similar to those in javascript. if you are already comfortable navigating the dom in javascript then this should be quite easy to pick up.

require('configure.php');
$table = 'demo_xml';
$query = "select * from $table";
$result = mysql_query($query);

to begin, establish a database connection and retrieve all values within the specified database table.

$xmldoc = new domdocument('1.0');

with our data readily available, we can begin creating our xml document using version 1.0.

$root = $xmldoc->createelement('music');

all xml documents require a root element and in this case, we are calling it “music”.

$root = $xmldoc->appendchild($root);

using the appendchildfunction, our new element will be written to the xml document.

while($row = mysql_fetch_assoc($result)) {

the while statement retrieves each row as an associative array with “name=value” pairs to make things easier later when looping through the data.

$rowcontainer = $xmldoc->createelement('song');
$rowcontainer = $root->appendchild($rowcontainer);

for each data pair, create a new row labeled “song” and write them to the xml document.

foreach ($row as $fieldname => $fieldvalue) {
	$child = $xmldoc->createelement($fieldname);
	$child = $rowcontainer->appendchild($child);
	$value = $xmldoc->createtextnode($fieldvalue);
	$value = $child->appendchild($value);
}

for every row available, insert each field name and its value.

$xmlstring = $xmldoc->savexml();
echo $xmlstring;
$xmldoc->save("songs.xml")

completing the script, the xml document is saved as a string and when viewed in a browser the values will be displayed. finally, save the the xml file as “songs.xml”.

in its entirety, the script below handles the complete output of the xml document.

<?php 

require('configure.php');
$table = 'demo_xml';
$query = "select * from $table";
$result = mysql_query($query);

$xmldoc = new domdocument('1.0');
$root = $xmldoc->createelement('music');
$root = $xmldoc->appendchild($root);

while($row = mysql_fetch_assoc($result)) {
	$rowcontainer = $xmldoc->createelement('song');
  	$rowcontainer = $root->appendchild($xmldoc);

	foreach ($row as $fieldname => $fieldvalue) {
		$child = $xmldoc->createelement($fieldname);
		$child = $rowcontainer->appendchild($child);
		$value = $xmldoc->createtextnode($fieldvalue);
	        $value = $child->appendchild($value);
	}
}

$xmlstring = $xmldoc->savexml();
echo $xmlstring;
$xmldoc->save("songs.xml")

?>

Leave a Reply

Your email address will not be published. Required fields are marked *