[ Index ]

PHP Cross Reference of MyBB 1.6.5

title

Body

[close]

/inc/ -> class_xml.php (source)

   1  <?php
   2  /**
   3   * MyBB 1.6
   4   * Copyright 2010 MyBB Group, All Rights Reserved
   5   *
   6   * Website: http://mybb.com
   7   * License: http://mybb.com/about/license
   8   *
   9   * $Id: class_xml.php 5297 2010-12-28 22:01:14Z Tomm $
  10   */
  11  
  12  /**
  13   * The following class is based upon code by Eric Pollman
  14   * @ http://eric.pollman.net/work/public_domain/
  15   * and is licensed under the public domain license.
  16   */
  17  
  18  class XMLParser {
  19      
  20      public $data;
  21      public $vals;
  22      public $collapse_dups = 1;
  23      public $index_numeric = 0;
  24  
  25      /**
  26       * Initialize the parser and store the XML data to be parsed.
  27       */
  28  	function __construct($data)
  29      {
  30          $this->data = $data;
  31      }
  32  
  33      /**
  34       * Build a tree based structure based from the parsed data
  35       *
  36       * @return array The tree based structure
  37       */
  38  	function get_tree()
  39      {
  40          $parser = xml_parser_create();
  41          xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
  42          xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
  43          if(!xml_parse_into_struct($parser, $this->data, $vals, $index))
  44          {
  45              return false;
  46          }
  47  
  48          $i = -1;
  49          return $this->get_children($vals, $i);
  50      }
  51  
  52      /**
  53       * Private: Build a completed tag by fetching all child nodes and attributes
  54       *
  55       * @param array Array of values from the current tag
  56       * @param array Array of child nodes
  57       * @param int Internal counter
  58       * @param string Type of tag. Complete is a single line tag with attributes
  59       * @return array Completed tag array
  60       */
  61  	function build_tag($thisvals, $vals, &$i, $type)
  62      {
  63          $tag['tag'] = $thisvals['tag'];
  64          if(isset($thisvals['attributes']))
  65          {
  66              $tag['attributes'] = $thisvals['attributes'];
  67          }
  68  
  69          if($type == "complete")
  70          {
  71              $tag['value'] = $thisvals['value'];
  72          }
  73          else
  74          {
  75              $tag = array_merge($tag, $this->get_children($vals, $i));
  76          }
  77          return $tag;
  78      }
  79      
  80      /**
  81       * Fetch the children for from a specific node array
  82       *
  83       * @param array Array of children
  84       * @param int Internal counter
  85       * @return array Array of child nodes
  86       */
  87  	function get_children($vals, &$i)
  88      {
  89          $children = array();
  90  
  91          if($i > -1 && isset($vals[$i]['value']))
  92          {
  93              $children['value'] = $vals[$i]['value'];
  94          }
  95  
  96          while(++$i < count($vals))
  97          {
  98              $type = $vals[$i]['type'];
  99              if($type == "cdata")
 100              {
 101                  $children['value'] .= $vals[$i]['value'];
 102              }
 103              elseif($type == "complete" || $type == "open")
 104              {
 105                  $tag = $this->build_tag($vals[$i], $vals, $i, $type);
 106                  if($this->index_numeric)
 107                  {
 108                      $tag['tag'] = $vals[$i]['tag'];
 109                      $children[] = $tag;
 110                  }
 111                  else
 112                  {
 113                      $children[$tag['tag']][] = $tag;
 114                  }
 115              }
 116              else if($type == "close")
 117              {
 118                  break;
 119              }
 120          }
 121          if($this->collapse_dups)
 122          {
 123              foreach($children as $key => $value)
 124              {
 125                  if(is_array($value) && (count($value) == 1))
 126                  {
 127                      $children[$key] = $value[0];
 128                  }
 129              }
 130          }
 131          return $children;
 132      }
 133  }
 134  
 135  /**
 136   * Kill off unnecessary tags and return a clean array of XML data
 137   *
 138   * @param array Array of parsed XML data
 139   * @return array Cleaned array of XML data
 140   */
 141  function kill_tags($array)
 142  {
 143      foreach($array as $key => $val)
 144      {
 145          if($key == "tag" || $key == "value")
 146          {
 147              unset($array[$key]);
 148          }
 149          else if(is_array($val))
 150          {
 151              // kill any nested tag or value indexes
 152              $array[$key] = kill_tags($val);
 153  
 154              // if the array no longer has any key/val sets
 155              // and therefore is at the deepest level, then
 156              // store the string value
 157              if(count($array[$key]) <= 0)
 158              {
 159                  $array[$key] = $val['value'];
 160              }
 161          }
 162      }
 163  
 164      return $array;
 165  }
 166  ?>


Generated: Sun Dec 11 14:16:27 2011 Cross-referenced by PHPXref 0.7.1