[ Index ]

PHP Cross Reference of MyBB

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$
  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 = array('tag' => $thisvals['tag']);
  64  
  65          if(isset($thisvals['attributes']))
  66          {
  67              $tag['attributes'] = $thisvals['attributes'];
  68          }
  69  
  70          if($type == "complete")
  71          {
  72              if(isset($thisvals['value']))
  73              {
  74                  $tag['value'] = $thisvals['value'];
  75              }
  76          }
  77          else
  78          {
  79              $tag = array_merge($tag, $this->get_children($vals, $i));
  80          }
  81          return $tag;
  82      }
  83      
  84      /**
  85       * Fetch the children for from a specific node array
  86       *
  87       * @param array Array of children
  88       * @param int Internal counter
  89       * @return array Array of child nodes
  90       */
  91  	function get_children($vals, &$i)
  92      {
  93          $children = array();
  94  
  95          if($i > -1 && isset($vals[$i]['value']))
  96          {
  97              $children['value'] = $vals[$i]['value'];
  98          }
  99  
 100          while(++$i < count($vals))
 101          {
 102              $type = $vals[$i]['type'];
 103              if($type == "cdata")
 104              {
 105                  $children['value'] .= $vals[$i]['value'];
 106              }
 107              elseif($type == "complete" || $type == "open")
 108              {
 109                  $tag = $this->build_tag($vals[$i], $vals, $i, $type);
 110                  if($this->index_numeric)
 111                  {
 112                      $tag['tag'] = $vals[$i]['tag'];
 113                      $children[] = $tag;
 114                  }
 115                  else
 116                  {
 117                      $children[$tag['tag']][] = $tag;
 118                  }
 119              }
 120              else if($type == "close")
 121              {
 122                  break;
 123              }
 124          }
 125          if($this->collapse_dups)
 126          {
 127              foreach($children as $key => $value)
 128              {
 129                  if(is_array($value) && (count($value) == 1))
 130                  {
 131                      $children[$key] = $value[0];
 132                  }
 133              }
 134          }
 135          return $children;
 136      }
 137  }
 138  
 139  /**
 140   * Kill off unnecessary tags and return a clean array of XML data
 141   *
 142   * @param array Array of parsed XML data
 143   * @return array Cleaned array of XML data
 144   */
 145  function kill_tags($array)
 146  {
 147      foreach($array as $key => $val)
 148      {
 149          if($key == "tag" || $key == "value")
 150          {
 151              unset($array[$key]);
 152          }
 153          else if(is_array($val))
 154          {
 155              // kill any nested tag or value indexes
 156              $array[$key] = kill_tags($val);
 157  
 158              // if the array no longer has any key/val sets
 159              // and therefore is at the deepest level, then
 160              // store the string value
 161              if(count($array[$key]) <= 0)
 162              {
 163                  $array[$key] = $val['value'];
 164              }
 165          }
 166      }
 167  
 168      return $array;
 169  }
 170  ?>


Generated: Tue Oct 8 19:19:50 2013 Cross-referenced by PHPXref 0.7.1