1<?php 2 /******************************************************** 3 * Class Name : XML-Reader * 4 * Publish On : June 20th, 2003 * 5 * Producer : Hermawan Haryanto (http://hermawan.com) * 6 * Version : 1.0 * 7 * License : GPL (General Public License) * 8 * * 9 * Credits: * 10 * 1. Hans Anderson (me@hansanderson.com) * 11 * http://www.hansanderson.com * 12 * 2. John Doe (acebone@f2s.com) * 13 ********************************************************/ 14 15class xmlreader { 16 var $_data; 17 var $_white; 18 var $_xml_url; 19 function xmlreader ($xml_url = "") 20 { 21 $this->_white = 1; 22 if (trim($xml_url) != "") $this->set_xml_url ($xml_url); 23 } 24 function set_xml_url ($url) 25 { 26 $this->_xml_url = $url; 27 } 28 function read () 29 { 30 if (!$this->_xml_url) $this->error ("XML File is not assigned."); 31 $fp = fopen ($this->_xml_url, "r"); 32 while (!feof ($fp)) $this->_data .= fgets($fp, 4096); 33 fclose ($fp); 34 $this->_data = trim ($this->_data); 35 } 36 function parse () 37 { 38 $this->read(); 39 if (trim ($this->_data) == "") $this->error ("Data not ready."); 40 $vals = $index = $array = array(); 41 $parser = xml_parser_create(); 42 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 43 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $this->_white); 44 xml_parse_into_struct($parser, $this->_data, $vals, $index); 45 xml_parser_free($parser); 46 $i = 0; 47 $tagname = $vals[$i]['tag']; 48 if ( isset ($vals[$i]['attributes'] ) ) 49 { 50 $array[$tagname]['@'] = $vals[$i]['attributes']; 51 } 52 else 53 { 54 $array[$tagname]['@'] = array(); 55 } 56 $array[$tagname]["#"] = $this->xml_depth($vals, $i); 57 return $array; 58 } 59 function xml_depth($vals, &$i) { 60 $children = array(); 61 if ( isset($vals[$i]['value']) ) 62 { 63 array_push($children, $vals[$i]['value']); 64 } 65 while (++$i < count($vals)) { 66 switch ($vals[$i]['type']) { 67 case 'open': 68 if ( isset ( $vals[$i]['tag'] ) ) 69 { 70 $tagname = $vals[$i]['tag']; 71 } 72 else 73 { 74 $tagname = ''; 75 } 76 if ( isset ( $children[$tagname] ) ) 77 { 78 $size = sizeof($children[$tagname]); 79 } 80 else 81 { 82 $size = 0; 83 } 84 if ( isset ( $vals[$i]['attributes'] ) ) 85 { 86 $children[$tagname][$size]['@'] = $vals[$i]["attributes"]; 87 } 88 $children[$tagname][$size]['#'] = $this->xml_depth($vals, $i); 89 break; 90 case 'cdata': 91 array_push($children, $vals[$i]['value']); 92 break; 93 case 'complete': 94 $tagname = $vals[$i]['tag']; 95 if( isset ($children[$tagname]) ) 96 { 97 $size = sizeof($children[$tagname]); 98 } 99 else 100 { 101 $size = 0; 102 } 103 if( isset ( $vals[$i]['value'] ) ) 104 { 105 $children[$tagname][$size]["#"] = $vals[$i]['value']; 106 } 107 else 108 { 109 $children[$tagname][$size]["#"] = ''; 110 } 111 if ( isset ($vals[$i]['attributes']) ) 112 { 113 $children[$tagname][$size]['@'] = $vals[$i]['attributes']; 114 } 115 break; 116 case 'close': 117 return $children; 118 break; 119 } 120 } 121 return $children; 122 } 123 function traverse_xmlize($array, $arrName = "array", $level = 0) { 124 foreach($array as $key=>$val) 125 { 126 if ( is_array($val) ) 127 { 128 traverse_xmlize($val, $arrName . "[" . $key . "]", $level + 1); 129 } 130 else 131 { 132 $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n"; 133 } 134 } 135 return 1; 136 } 137 function error ($str) 138 { 139 print get_class ($this)." ".$this->version()." => $str"; 140 exit(); 141 } 142 function version () 143 { 144 return "1.0"; 145 } 146 }; 147