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

Source for file simple.php

Documentation is available at simple.php

  1. <?php
  2. /**
  3. * Shows simple usage of SaxFilters, using a single custom filter
  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. // Define a customer handler class - just displays stuff
  15. /* implements XML_SaxFilters_FilterInterface */
  16. {
  17.     // Parsed output stored here
  18.     var $output = '';
  19.     
  20.     // For whitespace indentation
  21.     var $indent = '';
  22.  
  23.     // Called when parsing starts
  24.     function startDoc()
  25.     {
  26.         $this->output.="Parsing started\n";
  27.     }
  28.     
  29.     // Opening tag handler
  30.     function open($tag,$attribs)
  31.     {
  32.         $this->output.=$this->indent.$tag;
  33.         $sep '';
  34.         if count($attribs> 0 )
  35.         {
  36.             $this->output.=' (';
  37.             foreach $attribs as $key => $value )
  38.             {
  39.                 $this->output.="$sep$key$value";
  40.                 $sep ', ';
  41.             }
  42.             $this->output.=')';
  43.         }
  44.         $this->output.="\n";
  45.         $this->addIndent();
  46.     }
  47.  
  48.     // Closing tag handler
  49.     function close($tag)
  50.     {
  51.         $this->removeIndent();    
  52.         $this->output.=$this->indent.$tag."\n";
  53.     }
  54.  
  55.     // Character data handler
  56.     function data($data)
  57.     {
  58.         $data trim($data);
  59.         if !empty($data) ) {
  60.             $this->output.=$this->indent.$data."\n";
  61.         }
  62.     }
  63.     
  64.     // Called at end of parsing
  65.     function endDoc()
  66.     {
  67.         $this->output.="Parsing finished\n";
  68.     }
  69.     
  70.     function addIndent()
  71.     {
  72.         $this->indent.="\t";
  73.     }
  74.     function removeIndent()
  75.     {
  76.         $this->indent = substr_replace($this->indent,'',0,1);
  77.     }
  78. }
  79.  
  80. //---------------------------------------------------------------------
  81. // A Simple XML document
  82. $doc = <<<EOD
  83. <?xml version="1.0"?>
  84. <dynamically_typed_languages>
  85.     <language name="PHP" version="4.3.2">
  86.         PHP is number 1 for building web based applications.
  87.         <url>http://www.php.net</url>
  88.     </language>
  89.     <language name="Python" version="2.2.3">
  90.         Python is number 1 for cross platform desktop applications.
  91.         <url>http://www.python.org</url>
  92.     </language>
  93.     <language name="Perl" version="5.8.0">
  94.         Perl is number 1 for text and batch processing.
  95.         <url>http://www.perl.org</url>
  96.     </language>
  97. </dynamically_typed_languages>
  98. EOD;
  99.  
  100. //---------------------------------------------------------------------
  101. // This is where the action takes place
  102.  
  103. // Create the parser (use native SAX extension, StringReader, XML document)
  104. $parser XML_SaxFilters_createParser('Expat','String',$doc);
  105.  
  106. // This uses PEAR::XML_HTMLSax instead
  107. // $parser = & XML_SaxFilters_createParser('HTMLSax','String',$doc);
  108.  
  109. // Instantiate the filter above
  110. $filter new SimpleFilter();
  111.  
  112. // Add the filter to the parser
  113. $parser->setChild($filter);
  114.  
  115. // Parse
  116. if $parser->parse() ) {
  117.     $error $parser->getError();
  118.     echo $error->getMessage();
  119. else {
  120.     echo '<pre>'.$filter->output.'</pre>';
  121. }
  122. ?>

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