XML_SaxFilters
[ class tree: XML_SaxFilters ] [ index: XML_SaxFilters ] [ all elements ]

Source for file xml2html.php

Documentation is available at xml2html.php

  1. <?php
  2. /*
  3. Shows a basic example of parsing an XML data set of the type generated by
  4. phpMyAdmin into an HTML table
  5. */
  6. # require_once 'XML/SaxFilters.php'; // This is the normal way to do it
  7.  
  8. # Done to help development
  9. if !@include_once 'XML/SaxFilters.php' {
  10.     define('XML_SAXFILTERS''../../');
  11.     include_once XML_SAXFILTERS . 'SaxFilters.php';
  12. }
  13.  
  14. //----------------------------------------------------------------------------
  15. /**
  16.  * Filter for converting an XML data set to HTML
  17.  * @access public
  18.  */
  19. /* implements XML_SaxFilters_FilterInterface */
  20. {
  21.     /**
  22.     * Name for output file
  23.     */
  24.     var $outfile;
  25.     
  26.     /**
  27.      * Switch to determine if we're in a dataset row
  28.      * @var boolean 
  29.      */
  30.     var $inRow = false;
  31.  
  32.     /**
  33.      * The current column name
  34.      * @var string 
  35.      */
  36.     var $column = '';
  37.  
  38.     /**
  39.      * Array of data from a row
  40.      * @var array 
  41.      */
  42.     var $row;
  43.     
  44.     function DataSetFilter($outfile{
  45.         $this->outfile = $outfile;
  46.     }
  47.     
  48.     /**
  49.     * Start of parsing
  50.     */
  51.     function startDoc({
  52.         // Include the writer output class
  53.         require_once(XML_SAXFILTERS.'SaxFilters/IO/FileWriter.php');    
  54.         
  55.         // Instantiate the FileWriter
  56.         $writer new XML_SaxFilters_IO_FileWriter($this->outfile);
  57.         
  58.         // Set the writer that the filter will use
  59.         $this->setWriter($writer);
  60.         
  61.         // Write some data
  62.         $writer->write("<table>\n");
  63.         $writer->write("  <caption>The Found Fathers</caption>\n");
  64.         $writer->write("  <tr>\n");
  65.         $writer->write("    <th>First Name</th>\n    <th>Last Name</th>\n");
  66.         $writer->write("  </tr>\n");    
  67.     }
  68.  
  69.     /**
  70.      * Start Element Handler
  71.      */
  72.     function open($name,$attribs)
  73.     {
  74.         if $name == 'founding_fathers' )
  75.         {
  76.             $this->inRow = true;
  77.             $this->writer->write("  <tr>\n");
  78.         }
  79.         else if $this->inRow )
  80.         {
  81.             $this->column = $name;
  82.         }
  83.     }
  84.  
  85.     /**
  86.      * End Element Handler
  87.      */
  88.     function close($name)
  89.     {
  90.         if $name == 'founding_fathers' )
  91.         {
  92.             $this->inRow = false;
  93.             $this->writer->write("  </tr>\n");
  94.         }
  95.         else if $this->inRow )
  96.         {
  97.             $this->column = '';
  98.         }
  99.     }
  100.  
  101.     /**
  102.      * Character Data Handler
  103.      */
  104.     function data($data)
  105.     {
  106.         if $this->inRow )
  107.         {
  108.             switch $this->column {
  109.                 case 'first':
  110.                 case 'last':
  111.                     $this->writer->write("    <td>".$data."</td>\n");
  112.                 break;
  113.             }
  114.         }
  115.     }
  116.     
  117.     /**
  118.     * End of parsing
  119.     */
  120.     function endDoc({
  121.         // Finish writing
  122.         $this->writer->write("</table>\n");
  123.         $this->writer->close();
  124.     }
  125. }
  126. //----------------------------------------------------------------------------
  127.  
  128. // Create ExpatParser, telling it to use a File stream on founding_fathers.xml
  129. $parser XML_SaxFilters_createParser('Expat','File','founding_fathers.xml');
  130.  
  131. // Assign the DataSetFilter to the parser
  132. $parser->setChild(new DataSetFilter('founding_fathers.html'));
  133.  
  134. // Parse the XML document
  135. $parser->parse();
  136.  
  137. // Display the written file
  138. readfile('founding_fathers.html');
  139. ?>

Documentation generated on Mon, 11 Mar 2019 10:15:11 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.