Building XML Trees With PHP - Making Friends And Influencing People
(Page 9 of 11 )
Now, while all this is fine and dandy, how about using all this new-found knowledge for something practical?
This next example does just that, demonstrating how the XMLTree class can be used to convert data stored in a MySQL database into an XML document, and write it to a file for later use. Here's the MySQL table I'll be using,
+----+------------------+-----------------------------------+-----------
+----+------------------+-----------------------------------+----
--+----------------+
|
id | name | address | tel
|fax
|
+----+------------------+-----------------------------------+-----------
+----+------------------+-----------------------------------+----
--+----------------+
|
1 | Sam Spade | 134, Dark Road, New York, NY |
1-800-TOUGH-GUY |1-234-587-3636
|
| 2 | The White Rabbit | Down The Rabbit Hole, Wonderland | 56-78-5467
|56-78-2537
|
| 3 | Jack Liliput | The Little House, Lilliput Island
| None
|None
|
+----+------------------+-----------------------------------+-----------
+----+------------------+-----------------------------------+----
--+----------------+
and here's what I want my target XML document to look like:
<?xml version="1.0"?>
<addressbook>
<item id="1">
<name>Sam
Spade</name>
<address>134, Dark Road, New York, NY</address>
<tel>1-800-TOUGH-GUY</tel>
<fax>1-234-587-3636</fax>
</item>
<item
id="2">
<name>The White Rabbit</name>
<address>Down The Rabbit
Hole, Wonderland</address>
<tel>56-78-5467</tel>
<fax>56-78-2537</fax>
</item>
<item id="3">
<name>Jack
Liliput</name>
<address>The
Little House, Lilliput Island</address>
<tel>None</tel>
<fax>None</fax>
</item>
</addressbook>
With XMLTree, accomplishing this is a matter of a few lines of code. Here they
are:
<?php
// include class
include("Tree.php");
// set output filename
$filename
= 'addressbook.xml';
// instantiate object
$tree = new XML_Tree();
// add
the
root element
$root =& $tree->addRoot("addressbook");
// open connection
to
database
$connection = mysql_connect("localhost", "joe", "secret") or die
("Unable
to
connect!");
// select database
mysql_select_db("db567") or die
("Unable
to
select database!");
// execute query
$query = "SELECT * FROM addressbook";
$result
= mysql_query($query) or die ("Error in query: $query. " .
mysql_error());
//
iterate through rows and print column data
while ($row = mysql_fetch_array($result))
{
//
add child elements
$item =& $root->addChild("item", NULL, array("id" =>
$row['id']));
$name
=& $item->addChild("name", $row['name']);
$address
=& $item->addChild("address",
$row['address']);
$tel =& $item->addChild("tel",
$row['tel']);
$fax =&
$item->addChild("fax", $row['fax']);
}
// close
database connection
mysql_close($connection);
//
open file
if (!$handle = fopen($filename,
'w'))
{
print "Cannot open file ($filename)";
exit;
}
//
write XML to file
if
(!fwrite($handle, $tree->get()))
{
print "Cannot write
to file ($filename)";
exit;
}
//
close file
fclose($handle);
?>
Pretty simple, once you know how it works. First, I've opened up a connection
to the database and retrieved all the records from the table. Then I've instantiated a new document tree and iterated over the result set, adding a new set of nodes to the tree at each iteration. Finally, once all the rows have been processed, the dynamically generated tree is written to a file for later use.
Next: Doing The Chameleon >>
More PHP Articles
More By icarus, (c) Melonfire