[ Index ]

PHP Cross Reference of osCMax 2.0.4

title

Body

[close]

/admin/includes/classes/ -> xml_5.php (source)

   1  <?php
   2  ###################################################################################

   3  #

   4  # XML Library, by Keith Devens, version 1.2b

   5  # http://keithdevens.com/software/phpxml

   6  #

   7  # This code is Open Source, released under terms similar to the Artistic License.

   8  # Read the license at http://keithdevens.com/software/license

   9  #

  10  ###################################################################################

  11  
  12  /*

  13   * Changes to this file by JCKeebaugh 2007/10/02 as follows: Removed call-time

  14   * pass-by-reference in eight places in the XML Class (six lines.) The original

  15   * six lines are commented out, just below the modified lines.

  16   * 

  17   * These changes are released under the license stated in the header.

  18   */
  19  
  20  ###################################################################################

  21  # XML_unserialize: takes raw XML as a parameter (a string)

  22  # and returns an equivalent PHP data structure

  23  ###################################################################################

  24  function & XML_unserialize(&$xml){
  25      $xml_parser = &new XML();
  26      $data = &$xml_parser->parse($xml);
  27      $xml_parser->destruct();
  28      return $data;
  29  }
  30  ###################################################################################

  31  # XML_serialize: serializes any PHP data structure into XML

  32  # Takes one parameter: the data to serialize. Must be an array.

  33  ###################################################################################

  34  function & XML_serialize(&$data, $level = 0, $prior_key = NULL){
  35      if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; }
  36      while(list($key, $value) = each($data))
  37          if(!strpos($key, ' attr')) #if it's not an attribute
  38              #we don't treat attributes by themselves, so for an empty element

  39              # that has attributes you still need to set the element to NULL

  40  
  41              if(is_array($value) and array_key_exists(0, $value)){
  42                  XML_serialize($value, $level, $key);
  43              }else{
  44                  $tag = $prior_key ? $prior_key : $key;
  45                  echo str_repeat("\t", $level),'<',$tag;
  46                  if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element
  47                      while(list($attr_name, $attr_value) = each($data["$key attr"]))
  48                          echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';
  49                      reset($data["$key attr"]);
  50                  }
  51  
  52                  if(is_null($value)) echo " />\n";
  53                  elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n";
  54                  else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";
  55              }
  56      reset($data);
  57      if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }
  58  }
  59  ###################################################################################

  60  # XML class: utility class to be used with PHP's XML handling functions

  61  ###################################################################################

  62  class XML{
  63      var $parser;   #a reference to the XML parser

  64      var $document; #the entire XML structure built up so far

  65      var $parent;   #a pointer to the current parent - the parent will be an array

  66      var $stack;    #a stack of the most recent parent at each nesting level

  67      var $last_opened_tag; #keeps track of the last tag opened.

  68  
  69  	function XML(){
  70           $this->parser = &xml_parser_create();
  71      xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
  72  //    xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, false);

  73          xml_set_object($this->parser, $this);
  74  //    xml_set_object(&$this->parser, &$this);

  75          xml_set_element_handler($this->parser, 'open', 'close');
  76  //    xml_set_element_handler(&$this->parser, 'open','close');

  77          xml_set_character_data_handler($this->parser, 'data');
  78  //    xml_set_character_data_handler(&$this->parser, 'data');

  79      }
  80  	function destruct(){ xml_parser_free($this->parser); }
  81  //  function destruct(){ xml_parser_free(&$this->parser); }

  82      function & parse(&$data){
  83          $this->document = array();
  84          $this->stack    = array();
  85          $this->parent   = &$this->document;
  86          return xml_parse($this->parser, $data, true) ? $this->document : NULL;
  87  //    return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL;

  88      }
  89  	function open(&$parser, $tag, $attributes){
  90          $this->data = ''; #stores temporary cdata

  91          $this->last_opened_tag = $tag;
  92          if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
  93              if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
  94                  #this is the third or later instance of $tag we've come across

  95                  $key = count_numeric_items($this->parent[$tag]);
  96              }else{
  97                  #this is the second instance of $tag that we've seen. shift around

  98                  if(array_key_exists("$tag attr",$this->parent)){
  99                      $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
 100                      unset($this->parent["$tag attr"]);
 101                  }else{
 102                      $arr = array(&$this->parent[$tag]);
 103                  }
 104                  $this->parent[$tag] = &$arr;
 105                  $key = 1;
 106              }
 107              $this->parent = &$this->parent[$tag];
 108          }else{
 109              $key = $tag;
 110          }
 111          if($attributes) $this->parent["$key attr"] = $attributes;
 112          $this->parent  = &$this->parent[$key];
 113          $this->stack[] = &$this->parent;
 114      }
 115  	function data(&$parser, $data){
 116          if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
 117              $this->data .= $data;
 118      }
 119  	function close(&$parser, $tag){
 120          if($this->last_opened_tag == $tag){
 121              $this->parent = $this->data;
 122              $this->last_opened_tag = NULL;
 123          }
 124          array_pop($this->stack);
 125          if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];
 126      }
 127  }
 128  function count_numeric_items(&$array){
 129      return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
 130  }
 131  ?>


Generated: Fri Jan 1 13:43:16 2010 Cross-referenced by PHPXref 0.7