Bearsampp 2026.9.23
Loading...
Searching...
No Matches
XMLParser.php
Go to the documentation of this file.
1<?php
16
30{
36
41 var $_root = null;
42
47 var $_dataStack = array();
48
53 var $_valStack = array();
54
59 var $_depth = 0;
60
65 var $encoding = 'ISO-8859-1';
66
70 function getData()
71 {
73 }
74
79 function parse($data)
80 {
81 if (!extension_loaded('xml')) {
82 include_once 'PEAR.php';
83 return PEAR::raiseError("XML Extension not found", 1);
84 }
85 $this->_dataStack = $this->_valStack = array();
86 $this->_depth = 0;
87
88 if (
89 strpos($data, 'encoding="UTF-8"')
90 || strpos($data, 'encoding="utf-8"')
91 || strpos($data, "encoding='UTF-8'")
92 || strpos($data, "encoding='utf-8'")
93 ) {
94 $this->encoding = 'UTF-8';
95 }
96
97 $xp = xml_parser_create($this->encoding);
98 xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, 0);
99 xml_set_object($xp, $this);
100 xml_set_element_handler($xp, 'startHandler', 'endHandler');
101 xml_set_character_data_handler($xp, 'cdataHandler');
102 if (!xml_parse($xp, $data)) {
103 $msg = xml_error_string(xml_get_error_code($xp));
104 $line = xml_get_current_line_number($xp);
105 xml_parser_free($xp);
106 include_once 'PEAR.php';
107 return PEAR::raiseError("XML Error: '$msg' on line '$line'", 2);
108 }
109 xml_parser_free($xp);
110 return true;
111 }
112
122 function startHandler($parser, $element, $attribs)
123 {
124 $this->_depth++;
125 $this->_dataStack[$this->_depth] = null;
126
127 $val = array(
128 'name' => $element,
129 'value' => null,
130 'type' => 'string',
131 'childrenKeys' => array(),
132 'aggregKeys' => array()
133 );
134
135 if (count($attribs) > 0) {
136 $val['children'] = array();
137 $val['type'] = 'array';
138 $val['children']['attribs'] = $attribs;
139 }
140
141 array_push($this->_valStack, $val);
142 }
143
150 function postProcess($data, $element)
151 {
152 return trim($data);
153 }
154
163 function endHandler($parser, $element)
164 {
165 $value = array_pop($this->_valStack);
166 $data = $this->postProcess($this->_dataStack[$this->_depth], $element);
167
168 // adjust type of the value
169 switch (strtolower($value['type'])) {
170 // unserialize an array
171 case 'array':
172 if ($data !== '') {
173 $value['children']['_content'] = $data;
174 }
175
176 $value['value'] = isset($value['children']) ? $value['children'] : array();
177 break;
178
179 /*
180 * unserialize a null value
181 */
182 case 'null':
183 $data = null;
184 break;
185
186 /*
187 * unserialize any scalar value
188 */
189 default:
190 settype($data, $value['type']);
191 $value['value'] = $data;
192 break;
193 }
194
195 $parent = array_pop($this->_valStack);
196 if ($parent === null) {
197 $this->_unserializedData = &$value['value'];
198 $this->_root = &$value['name'];
199 return true;
200 }
201
202 // parent has to be an array
203 if (!isset($parent['children']) || !is_array($parent['children'])) {
204 $parent['children'] = array();
205 if ($parent['type'] != 'array') {
206 $parent['type'] = 'array';
207 }
208 }
209
210 if (!empty($value['name'])) {
211 // there already has been a tag with this name
212 if (in_array($value['name'], $parent['childrenKeys'])) {
213 // no aggregate has been created for this tag
214 if (!in_array($value['name'], $parent['aggregKeys'])) {
215 if (isset($parent['children'][$value['name']])) {
216 $parent['children'][$value['name']] = array($parent['children'][$value['name']]);
217 } else {
218 $parent['children'][$value['name']] = array();
219 }
220 array_push($parent['aggregKeys'], $value['name']);
221 }
222 array_push($parent['children'][$value['name']], $value['value']);
223 } else {
224 $parent['children'][$value['name']] = &$value['value'];
225 array_push($parent['childrenKeys'], $value['name']);
226 }
227 } else {
228 array_push($parent['children'],$value['value']);
229 }
230 array_push($this->_valStack, $parent);
231
232 $this->_depth--;
233 }
234
243 function cdataHandler($parser, $cdata)
244 {
245 $this->_dataStack[$this->_depth] .= $cdata;
246 }
247}
startHandler($parser, $element, $attribs)
cdataHandler($parser, $cdata)
endHandler($parser, $element)
postProcess($data, $element)