• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List

E:/E/GEAMP/www/openbiz/openbiz/bin/util/xmltoarray.src.php

00001 <?php
00017 // Based on code found online at:
00018 // http://php.net/manual/en/function.xml-parse-into-struct.php
00019 //
00020 // Author: Eric Pollmann
00021 // Released into public domain September 2003
00022 //http://eric.pollmann.net/work/public_domain/
00023 /*
00024 //$parser = new XMLParser('./BOEvent.xml', 'file', 1);
00025 //$parser = new XMLParser('./FMSponsor_chart.xml', 'file', 1);
00026 //$parser = new XMLParser('./EventView.xml', 'file', 1);
00027 $parser = new XMLParser('./FMEvent.xml', 'file', 1);
00028 $tree = $parser->getTree();
00029 echo "<pre>";
00030 print_r($tree);
00031 echo "</pre>";
00032 */
00033 class XMLParser {
00034        var $data;           // Input XML data buffer
00035        var $vals;           // Struct created by xml_parse_into_struct
00036        var $collapse_dups;  // If there is only one tag of a given name,
00037                             //   shall we store as scalar or array?
00038        var $index_numeric;  // Index tags by numeric position, not name.
00039                             //   useful for ordered XML like CallXML.
00040 
00041        // Read in XML on object creation.
00042        // We can take raw XML data, a stream, a filename, or a url.
00043        function XMLParser($data_source, $data_source_type='raw', $collapse_dups=0, $index_numeric=0) {
00044               $this->collapse_dups = $collapse_dups;
00045               $this->index_numeric = $index_numeric;
00046               
00047               $this->data = '';
00048               if ($data_source_type == 'raw')
00049                      $this->data = $data_source;
00050 
00051               elseif ($data_source_type == 'stream') {
00052                      while (!feof($data_source))
00053                             $this->data .= fread($data_source, 1000);
00054 
00055               // try filename, then if that fails...
00056               } elseif (file_exists($data_source)) {
00057                      $this->data = implode('', file($data_source)); 
00058         }
00059               // try url
00060               else {
00061                      $fp = fopen($data_source,'r');
00062                      while (!feof($fp))
00063                             $this->data .= fread($fp, 1000);
00064                      fclose($fp);
00065               }
00066               
00067               //add support for load encoded files
00068               if(function_exists("ioncube_read_file"))
00069               {
00070                      $data = ioncube_read_file($data_source);
00071                      if (!is_int($data)) {
00072                             $this->data = $data;        
00073                      }
00074               }elseif(substr($this->data,0,7)=='!odMbo!')
00075               {
00076                      header("Location: ".APP_INDEX.'/common/loader_not_installed');
00077                      exit;
00078               }
00079        }
00080 
00081        // Parse the XML file into a verbose, flat array struct.
00082        // Then, coerce that into a simple nested array.
00083        function &getTree() {
00084            // load xml and check if it turns a valid object. TODO: it will throw error.
00085            /*$xml = simplexml_load_file($this->data);
00086            if (!$xml) {
00087                echo 'invalid xml file '.$this->data; exit;
00088            }*/
00089               //$parser = xml_parser_create('ISO-8859-1');
00090               $parser = xml_parser_create('');
00091               xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
00092               xml_parse_into_struct($parser, $this->data, $vals, $index); 
00093               xml_parser_free($parser);
00094 
00095               $i = -1;
00096               return $this->getchildren($vals, $i);
00097        }
00098        
00099        // internal function: build a node of the tree
00100        function buildtag($thisvals, $vals, &$i, $type) {
00101 
00102               if (isset($thisvals['attributes']))
00103                      $tag['ATTRIBUTES'] = $thisvals['attributes']; 
00104 
00105               // complete tag, just return it for storage in array
00106               if ($type === 'complete')
00107                      $tag['VALUE'] = $thisvals['value'];
00108 
00109               // open tag, recurse
00110               else
00111                      $tag = array_merge((array)$tag, (array)$this->getchildren($vals, $i));
00112 
00113               return $tag;
00114        }
00115 
00116        // internal function: build an nested array representing children
00117        function getchildren($vals, &$i) { 
00118               $children = array();     // Contains node data
00119 
00120               // Node has CDATA before it's children
00121                 if ($i > -1 && isset($vals[$i]['value']))
00122                      $children['VALUE'] = $vals[$i]['value'];
00123 
00124               // Loop through children, until hit close tag or run out of tags
00125               while (++$i < count($vals)) { 
00126 
00127                      $type = $vals[$i]['type'];
00128 
00129                      // 'cdata':   Node has CDATA after one of it's children
00130                      //            (Add to cdata found before in this case)
00131                      if ($type === 'cdata')
00132                             $children['VALUE'] .= $vals[$i]['value'];
00133 
00134                      // 'complete':       At end of current branch
00135                      // 'open':    Node has children, recurse
00136                      elseif ($type === 'complete' || $type === 'open') {
00137                             $tag = $this->buildtag($vals[$i], $vals, $i, $type);
00138                             if ($this->index_numeric) {
00139                                    $tag['TAG'] = $vals[$i]['tag'];
00140                                    $children[] = $tag;
00141                             } else
00142                                    $children[$vals[$i]['tag']][] = $tag;
00143                      }
00144 
00145                      // 'close:    End of node, return collected data
00146                      //            Do not increment $i or nodes disappear!
00147                      elseif ($type === 'close')
00148                             break;
00149               } 
00150               if ($this->collapse_dups)
00151                      foreach($children as $key => $value)
00152                             if (is_array($value) && (count($value) == 1))
00153                                    $children[$key] = $value[0];
00154               return $children;
00155        } 
00156 }
00157 
00158 ?>

Generated on Thu Apr 19 2012 17:09:14 for openbiz by  doxygen 1.7.2