2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright ©2002-[#]year[#] Blue Static
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
32 * This framework is a wrapper for a robust XML parser.
35 * @copyright Copyright ©2002 - [#]year[#], Blue Static
43 * Framework registry object
57 * An array of function names that are to be executed for each tag name (name => function)
61 var $taghandler = array();
71 * Tag stack of all open nodes
78 * Node list for all open tag attributes
82 var $attribs = array();
85 * Resulting parsed array
89 var $result = array();
91 // ###################################################################
95 function __construct(&$registry)
97 $this->registry
=& $registry;
100 // ###################################################################
102 * (PHP 4) Constructor
104 function XML(&$registry)
106 $this->__construct($registry);
109 // ###################################################################
115 * @param string XML file data
116 * @param string Parse file as UTF-8 instead of ISSO-8859-1
118 * @return array Array with all the XML data parsed
120 function parse($data, $utf8 = false
)
122 $this->stack
= array();
123 $this->attribs
= array();
124 $this->result
= array();
129 $data = utf8_encode($data);
130 $this->parser
= xml_parser_create('UTF-8');
134 $this->parser
= xml_parser_create('ISO-8859-1');
137 // create a new parser
138 xml_set_object($this->parser
, $this);
139 xml_parser_set_option($this->parser
, XML_OPTION_CASE_FOLDING
, 0);
140 xml_set_element_handler($this->parser
, 'handle_tag_start', 'handle_tag_end');
141 xml_set_character_data_handler($this->parser
, 'handle_cdata');
143 $this->attach_node($this->result
);
145 // parse the data and check for errors
146 if (!xml_parse($this->parser
, $data))
148 $error['code'] = xml_get_error_code($this->parser
);
149 $error['string'] = xml_error_string($error['code']);
150 $error['line'] = xml_get_current_line_number($this->parser
);
151 $error['column'] = xml_get_current_column_number($this->parser
);
152 trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]", E_USER_ERROR
);
156 // destroy the parser
157 xml_parser_free($this->parser
);
159 // done... send the results back
160 return $this->result
;
163 // ###################################################################
165 * Process the opening location of an XML tag
169 * @param integer XML parser
170 * @param string Tag name
171 * @param array Tag attributes
173 function handle_tag_start(&$parser, $name, $attrs)
175 // we need to keep track of indicies to monitor the last key in $this->attribs
179 array_walk($attrs, 'trim');
182 if (isset($this->attribs
["$name"]))
184 // node set has < 1 child nodes
185 if (!isset($this->attribs
["$name"][0]))
187 $tmp = $this->attribs
["$name"];
188 unset($this->attribs
["$name"]);
189 $this->attribs
["$name"][0] = $tmp;
192 // create a new child node
193 $this->attribs
["$name"][ $index["$name"] ] = (array)$attrs;
194 $this->attach_node($this->attribs
["$name"][ $index["$name"] ]);
197 // node set doesn't exist, so create it
200 $this->attribs
["$name"] = (array)$attrs;
201 $this->attach_node($this->attribs
["$name"]);
206 // ###################################################################
212 * @param integer XML parser
213 * @param string CDATA from tag
215 function handle_cdata(&$parser, $data)
217 $this->cdata
.= $data;
220 // ###################################################################
222 * Process the closing of an XML tag
226 * @param integer XML parser
227 * @param string Tag name
229 function handle_tag_end(&$parser, $name)
231 // attach data to the node
232 if (($this->cdata
= trim($this->cdata
)) != '')
234 $this->attribs
['value'] = $this->cdata
;
240 $this->detach_node();
243 // ###################################################################
245 * Shifts the node tree
249 * @param array Node to place into the stack
251 function attach_node(&$node)
254 $this->stack
[ sizeof($this->stack
) ] =& $node;
256 // new attributes to work with
257 $this->attribs
=& $node;
260 // ###################################################################
262 * Unshifts the node tree
266 function detach_node()
268 // drop the newest node
269 unset($this->stack
[ sizeof($this->stack
) - 1 ]);
271 // assign the attributes to the next newest node
272 $this->attribs
=& $this->stack
[ sizeof($this->stack
) - 1 ];
275 // ###################################################################
277 * Unless a node has multiple children, there will not be a numerical
278 * index for the child node. So this means that if you have variable
279 * XML structure with some of the same types of nodes having one children
280 * or multiple children, you'd have different things to parse. If
281 * you want the node unified (as in, all single-children would be indexed
282 * numerically, run this function on the node. It works on references.
286 * @param array The node to int-index
288 function unify_node(&$node)
290 if (!isset($node[0]))
292 $node = array($node);
297 /*=====================================================================*\
298 || ###################################################################
301 || ###################################################################
302 \*=====================================================================*/