Home arrow PHP arrow Page 9 - Building XML Trees With PHP

Making Friends And Influencing People - PHP

Need to manipulate XML document trees, but don't have the DOM extension compiled into your PHP build? Take a look at XMLTree, a PEAR class that allows you to create and manipulate XML document trees without requiring the PHP DOM extension.

TABLE OF CONTENTS:
  1. Building XML Trees With PHP
  2. A Hero Is Born
  3. Anatomy Class
  4. A La Carte
  5. Slice And Dice
  6. Killing Off The Kids
  7. Rank And File
  8. Spider, Spider On The Wall...
  9. Making Friends And Influencing People
  10. Doing The Chameleon
  11. Linking Out
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 27
February 20, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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.

 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: