PHP
  Home arrow PHP arrow Page 9 - Building XML Trees With PHP
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Building XML Trees With PHP
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 24
    2003-02-20

    Table of Contents:
  • Building XML Trees With PHP
  • A Hero Is Born
  • Anatomy Class
  • A La Carte
  • Slice And Dice
  • Killing Off The Kids
  • Rank And File
  • Spider, Spider On The Wall...
  • Making Friends And Influencing People
  • Doing The Chameleon
  • Linking Out

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More PHP Articles
    More By icarus, (c) Melonfire


     

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway