00001 <?
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 class XmlParser
00021 {
00023 var $filename;
00024
00026 var $content;
00027
00029 function XmlParser($filename)
00030 {
00031
00032
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 ?>