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

Source for file chained.php

Documentation is available at chained.php

  1. <?php
  2. /**
  3. * Shows two filters chained together
  4. */
  5. # require_once 'XML/SaxFilters.php'; // This is the normal way to do it
  6.  
  7. # Done to help development
  8. if !@include_once 'XML/SaxFilters.php' {
  9.     define('XML_SAXFILTERS''../../');
  10.     include_once XML_SAXFILTERS . 'SaxFilters.php';
  11. }
  12.  
  13. //---------------------------------------------------------------------
  14. /**
  15. * Make a seperate class for both filters to write output to
  16. */
  17. class Output {
  18.     var $output '';
  19.     var $indent '';
  20.     function writeLine($text{
  21.         $this->output.=$this->indent.$text."\n";
  22.     }
  23.     function fetch({
  24.         return $this->output;
  25.     }
  26.     function addIndent({
  27.         $this->indent.="\t";
  28.     }
  29.     function removeIndent({
  30.         $this->indent substr_replace($this->indent,'',0,1);
  31.     }
  32. }
  33.  
  34. //---------------------------------------------------------------------
  35. // Filters for Language tag
  36. /* implements XML_SaxFilters_FilterInterface */
  37. {
  38.     /**
  39.     * Instance of Output
  40.     */
  41.     var $Output;
  42.     
  43.     var $inLanguage = FALSE;
  44.     
  45.     function LanguageFilter($Output{
  46.         $this->Output = $Output;
  47.     }
  48.    
  49.     // Opening tag handler
  50.     function open($tag,$attribs{
  51.         // Call child filter
  52.         $this->child->open($tag,$attribs);
  53.         
  54.         if $tag == 'language' {
  55.             $this->inLanguage = TRUE;
  56.             if !isset($attribs['name']) ) $attribs['name''Unknown';
  57.             if !isset($attribs['version']) ) $attribs['version''Unknown';
  58.             $this->Output->writeLine($attribs['name'].' version '.$attribs['version']);
  59.             $this->Output->addIndent();        
  60.         }
  61.     }
  62.  
  63.     // Closing tag handler
  64.     function close($tag{
  65.         // Call child filter
  66.         $this->child->close($tag);
  67.         
  68.         if $tag == 'language' {
  69.             $this->inLanguage = FALSE;
  70.             $this->Output->removeIndent();
  71.         }
  72.     }
  73.  
  74.     // Character data handler
  75.     function data($data{
  76.         // Call child filter
  77.         if !$this->child->data($data) ) {
  78.             if $this->inLanguage {
  79.                 $this->Output->writeLine($data);
  80.             }
  81.         }
  82.     }
  83. }
  84. //---------------------------------------------------------------------
  85. // Filters for URL
  86. /* implements XML_SaxFilters_FilterInterface */
  87. {
  88.     /**
  89.     * Instance of Output
  90.     */
  91.     var $Output;
  92.     
  93.     var $inUrl = FALSE;
  94.     
  95.     function UrlFilter($Output{
  96.         $this->Output = $Output;
  97.     }
  98.    
  99.     // Opening tag handler
  100.     function open($tag,$attribs{       
  101.         if $tag == 'url' {
  102.             $this->inUrl = TRUE;
  103.         }
  104.     }
  105.  
  106.     // Closing tag handler
  107.     function close($tag{
  108.         if $tag == 'url' {
  109.             $this->inUrl = FALSE;
  110.         }
  111.     }
  112.  
  113.     // Character data handler
  114.     function data($data{
  115.         if $this->inUrl {
  116.             $this->Output->writeLine($data);
  117.             return TRUE;
  118.         else {
  119.             return FALSE;
  120.         }
  121.     }
  122. }
  123. //---------------------------------------------------------------------
  124. // A Simple XML document
  125. $doc = <<<EOD
  126. <?xml version="1.0"?>
  127. <dynamically_typed_languages>
  128.     <language name="PHP" version="4.3.2">
  129.         PHP is number 1 for building web based applications.
  130.         <url>http://www.php.net</url>
  131.     </language>
  132.     <language name="Python" version="2.2.3">
  133.         Python is number 1 for cross platform desktop applications.
  134.         <url>http://www.python.org</url>
  135.     </language>
  136.     <language name="Perl" version="5.8.0">
  137.         Perl is number 1 for text and batch processing.
  138.         <url>http://www.perl.org</url>
  139.     </language>
  140. </dynamically_typed_languages>
  141. EOD;
  142.  
  143. //---------------------------------------------------------------------
  144. // This is where the action takes place
  145.  
  146. // Create the parser (use native SAX extension, StringReader, XML document)
  147. $parser XML_SaxFilters_createParser('Expat','String',$doc);
  148.  
  149. // Create the instance of Output
  150. $Output new Output();
  151.  
  152. // Set up the filters
  153. $LF new LanguageFilter($Output);
  154. $UF new UrlFilter($Output);
  155.  
  156. // Add the UrlFilter to the LanguageFilter
  157. $LF->setChild($UF);
  158.  
  159. // Add the LanguageFilter to the parser
  160. $parser->setChild($LF);
  161.  
  162. // Parse
  163. if $parser->parse() ) {
  164.     $error $parser->getError();
  165.     echo $error->getMessage();
  166. else {
  167.     echo '<pre>'.$Output->fetch().'</pre>';
  168. }
  169. ?>

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