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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT