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

Doing The Chameleon - 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
Why stop there? It's also possible to pair the dynamically-built XML document in the previous example with an XSLT stylesheet to produce HTML output. Here's the stylesheet,


<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head></head> <body> <table border="1"> <tr> <td><b>Name</b></td> <td><b>Address</b></td> <td><b>Tel</b></td> <td><b>Fax</b></td> </tr> <xsl:apply-templates/> </table> </body> </html> </xsl:template> <xsl:template match="//addressbook/item"> <tr> <td><xsl:value-of select="name" /></td> <td><xsl:value-of select="address" /></td> <td><xsl:value-of select="tel" /></td> <td><xsl:value-of select="fax" /></td> </tr> </xsl:template> </xsl:stylesheet>
and here's a variant of the previous example which, instead of writing the XML to a file, passes it through PHP's XSLT processor to produce an HTML page containing the data from the MySQL database.

<?php // include class include("Tree.php"); // set XSLT file $xslt_file = "addressbook.xsl"; // 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); // read XML into string $xml = $tree->get(); // create the XSLT processor $xslt_processor = xslt_create(); // read in the data $xslt = join("", file($xslt_file)); // set up buffers $arg_buffer = array("/xml" => $xml, "/xslt" => $xslt); // create the XSLT processor $xp = xslt_create() or die("Could not create XSLT processor"); // process the two strings to get the desired output if($result = xslt_process($xp, "arg:/xml", "arg:/xslt", NULL, $arg_buffer)) { echo $result; } else { echo "An error occurred: " . xslt_error($xp) . "(error code " . xslt_errno($xp) . ")"; } // free the resources occupied by the handler xslt_free($xp); ?>
Here's what the output looks like:



Simple when you know how!

 
 
>>> 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: