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

Source for file XMLParser.php

Documentation is available at XMLParser.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4  */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 5                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2004 David Costa                                       |
  7. // | All rights reserved.                                                 |
  8. // +----------------------------------------------------------------------+
  9. // |                                                                      |
  10. // | Redistribution and use in source and binary forms, with or without   |
  11. // | modification, are permitted provided that the following conditions   |
  12. // | are met:                                                             |
  13. // |                                                                      |
  14. // | Redistributions of source code must retain the above copyright       |
  15. // | notice, this list of conditions and the following disclaimer.        |
  16. // |                                                                      |
  17. // | Redistributions in binary form must reproduce the above copyright    |
  18. // | notice, this list of conditions and the following disclaimer in the  |
  19. // | documentation and/or other materials provided with the distribution. |
  20. // |                                                                      |
  21. // | Neither the name of David Costa nor the names of his contributors may|
  22. // | be used to endorse or promote products derived from this software    |
  23. // | without specific prior written permission.                           |
  24. // |                                                                      |
  25. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  26. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  27. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  28. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  29. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  30. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  31. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  32. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  33. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  34. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  35. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  36. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  37. // +----------------------------------------------------------------------+
  38. // | Authors: David Costa     <gurugeek@php.net>                          |
  39. // | Authors: <Ashley Hewson <morbidness@gmail.com>                       |
  40. // +----------------------------------------------------------------------+
  41. //$Id: XMLParser.php,v 1.5 2004/09/29 12:20:26 gurugeek Exp $  $
  42.  
  43.  
  44. // more comments will be added soon. Alpha version of the XML parser
  45. // to be possibly replaced by a proper parse or extension as suggested by
  46. // Stephan Schimdt 
  47.  
  48. class DB_Sqlite_Tools_XMLParser {
  49.         public $fh;
  50.         public $element;
  51.         public $enclosed;
  52.         public $ignoreList = array();
  53.         private $str;
  54.         
  55.         public function __construct($fh$pos = 0{
  56.             $this->fh $fh;
  57.             fseek($fh$pos);
  58.             $this->strlen strlen($this->str);
  59.         }
  60.         public function getNextElement(
  61.         {
  62.             // the loop is so that if we are told to ignore a certain
  63.             // element, then we can continue on to the next one and
  64.             // return that.
  65.             while (true{
  66.                 // obviously, if we're at EOF, then there are no more
  67.                 // elements ;)
  68.                 if (feof($this->fh)) return false;
  69.                 // read up to the first open bracket, storing what's
  70.                 // in between
  71.                 $this->enclosed "";
  72.                 $c fgetc($this->fh);
  73.                 while (($c != '<'&& !feof($this->fh)) {
  74.                     $this->enclosed.= $c;
  75.                     $c fgetc($this->fh);
  76.                 }
  77.                 // read up to the first close bracket that isn't within
  78.                 // quote marks, storing what's in between.
  79.                 $this->element "";
  80.                 $inQuote = false;
  81.                 $c fgetc($this->fh);
  82.                 while (($c != '>'// end if $c == '>'
  83.                  || ($inQuote// unless this is within a quote,
  84.                  || (feof($this->fh)) // or if we have reached EOF.
  85.                 {
  86.                     // toggle quote flag
  87.                     if ($c == '"'$inQuote !$inQuote;
  88.                     $this->element.= $c;
  89.                     $c fgetc($this->fh);
  90.                 }
  91.                 // default action is to accept this element, however we have
  92.                 // to check it against the list of elements to ignore, like
  93.                 // <!-- -->
  94.                 $break = true;
  95.                 foreach($this->ignoreList as $ignore{
  96.                     if (preg_match("/$ignore/"$this->getElement())) $break = false;
  97.                 }
  98.                 // break the while loop if this is an acceptable element
  99.                 if ($breakbreak;
  100.             }
  101.             return true;
  102.         }
  103.         public function ignore($str
  104.         {
  105.             // add $str to ignore list
  106.             $this->ignoreList[$str;
  107.         }
  108.         public function getElement(
  109.         {
  110.             return trim($this->element);
  111.         }
  112.         public function getEnclosed(
  113.         {
  114.             return $this->enclosed;
  115.         }
  116.         public function getElementAttribute($name
  117.         {  
  118.             $el $this->getElement();
  119.             preg_match('/[ ]+'.$name.'[ ]*=[ ]*"([^"]*)"/'$el$result);
  120.             return $result[1];
  121.         }
  122.         public function getElementName(
  123.         {
  124.             preg_match("/^([^ ]*).*$/"$this->getElement()$result);
  125.             #echo $result[1] ;
  126.             return $result[1];
  127.         }
  128.     }
  129. ?>

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