Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

XmlParser.class.php

Go to the documentation of this file.
00001 <?
00002 /* XmlParser.class.php
00003 
00004    Class to manage XML files. Its main aim is to encapsulate all the 
00005    stuff related to XML management and to provide a wrapper to the
00006    XML functions in PHP to make them OO and to allow an easy
00007    transition to PHP 5.
00008    
00009    Its main functions are:
00010         - (TODO) Given an XSchema, Check if an XML file is valid.
00011         - Parse the XML file into a struct via xml_parse_into_struct.
00012         - Parse the XML file into a tree (it allows an easier processing
00013           than working with the struct).
00014         - (TODO) Apply an XSLT to a given XML file.
00015         
00016    Note: The parse XML into a tree code is taken from the one by 
00017    [dave at ovumdesign dot com] in the PHP manual.
00018 */
00019 
00020 class XmlParser
00021 {
00023         var $filename;
00024         
00026         var $content;
00027         
00029         function XmlParser($filename)
00030         {
00031                 /* If it is the name of a file, we will suppose we want to
00032                 parse it, else, it will be the Xml data */
00033                 if (file_exists($filename))
00034                         $this->setFilename($filename);
00035                 else
00036                         $this->setData($filename);
00037         }
00038         
00040         function setFilename($string)
00041         {
00042                 $this->filename = $string;
00043                 
00044                 $this->content = join('', file($this->filename));
00045         }
00046         
00048         function setData($data)
00049         {
00050                 $this->content = $data;
00051         }
00052         
00055         function &getStruct()
00056         {
00057                 $parser = xml_parser_create();
00058                 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
00059                 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE,   1);
00060                 xml_parse_into_struct($parser, $this->content, $vals, $index);
00061                 xml_parser_free($parser);
00062                 
00063                 $struct["vals"] =& $vals;
00064                 $struct["index"] =& $index;
00065                 
00066                 return $struct;
00067         }
00068         
00074         function &getTree()
00075         {
00076                 $struct =& $this->getStruct();
00077                 
00078                 
00079                 return array(
00080                         'tag'       => $struct["vals"][0]['tag'],
00081                         'attributes' => isset($struct["vals"][0]['attributes'])?
00082                                                 $struct["vals"][0]['attributes'] : null,
00083                         'children'   => $this->_getChildren($struct["vals"], $i = 0),
00084                 );              
00085         }
00086         
00087         function _getChildren($vals, &$i) 
00088         {
00089                 $children = array();
00090                 if (isset($vals[$i]['value'])) $children[] = $vals[$i]['value'];
00091 
00092                 while (++$i < count($vals)) 
00093                 {
00094                         switch ($vals[$i]['type']) 
00095                         {
00096                                 case 'cdata':
00097                                         $children[] = $vals[$i]['value'];
00098                                         break;
00099 
00100                                 case 'complete':
00101                                         $children[] = array(
00102                                                 'tag'       => $vals[$i]['tag'],
00103                                                 'attributes' => isset($vals[$i]['attributes'])?
00104                                                                 $vals[$i]['attributes'] : null,
00105                                                 'value'      => $vals[$i]['value'],
00106                                         );
00107                                         break;
00108 
00109                                 case 'open':
00110                                         $children[] = array(
00111                                                 'tag'      => $vals[$i]['tag'],
00112                                                 'attributes' => isset($vals[$i]['attributes'])?
00113                                                                 $vals[$i]['attributes'] : null,
00114                                                 'children'   => $this->_getchildren($vals, $i),
00115                                         );
00116                                         break;
00117 
00118                                 case 'close':
00119                                         return $children;
00120                         }
00121                 }
00122         }               
00123 }
00124 ?>

Generated on Wed Nov 19 20:29:35 2003 for James by doxygen 1.3.4