]>
src.bluestatic.org Git - isso.git/blob - xml.php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
20 $OBJECT = 'XML Parser';
21 $CLASS = 'XML_Parser';
27 * This framework is a wrapper for a robust XML parser.
29 * @author Iris Studios, Inc.
30 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
45 * An array of function names that are to be executed for each tag name (name => function)
49 var $taghandler = array();
58 * Tag stack of all open nodes
64 * Node list for all open tag attributes
67 var $attribs = array();
70 * Resulting parsed array
73 var $result = array();
78 * @param str XML file data
80 * @return array Array with all the XML data parsed
84 $this->stack
= array();
85 $this->attribs
= array();
86 $this->result
= array();
89 // create a new parser
90 $this->parser
= xml_parser_create();
91 xml_set_object($this->parser
, $this);
92 xml_parser_set_option($this->parser
, XML_OPTION_CASE_FOLDING
, 0);
93 xml_set_element_handler($this->parser
, 'handle_tag_start', 'handle_tag_end');
94 xml_set_character_data_handler($this->parser
, 'handle_cdata');
96 $this->attach_node($this->result
);
98 // parse the data and check for errors
99 if (!xml_parse($this->parser
, $data))
101 $error['code'] = xml_get_error_code($this->parser
);
102 $error['string'] = xml_error_string($error['code']);
103 $error['line'] = xml_get_current_line_number($this->parser
);
104 $error['column'] = xml_get_current_column_number($this->parser
);
105 trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]", E_USER_ERROR
);
109 // destroy the parser
110 xml_parser_free($this->parser
);
112 // done... send the results back
113 return $this->result
;
117 * Process the opening location of an XML tag
119 * @param res XML parser
120 * @param str Tag name
121 * @param array Tag attributes
123 function handle_tag_start(&$parser, $name, $attrs)
126 array_walk($attrs, 'trim');
129 if (isset($this->attribs
["$name"]))
131 // node set has < 1 child nodes
132 if (!isset($this->attribs["$name"][0]))
134 $tmp = $this->attribs
["$name"];
135 unset($this->attribs["$name"]);
136 $this->attribs
["$name"][0] = $tmp;
139 // create a new child node
140 $this->attach_node($this->attribs["$name"][] = (array)$attrs);
142 // node set doesn't exist, so create it
145 $this->attach_node($this->attribs
["$name"] = (array)$attrs);
152 * @param res XML parser
153 * @param str CDATA from tag
155 function handle_cdata(&$parser, $data)
157 $this->cdata .= $data;
161 * Process the closing of an XML tag
163 * @param res XML parser
164 * @param str Tag name
166 function handle_tag_end(&$parser, $name)
170 // attach data to the node
171 if (($this->cdata = trim($this->cdata)) != '')
173 // if we have a data handler, operate it now
174 if (isset($this->taghandler["$name"]))
176 $_isso->debug("handler: " . $this->taghandler
["$name"]);
177 if (function_exists($this->taghandler["$name"]))
179 $this->cdata
= $this->taghandler
["$name"]($this->cdata, $this);
183 trigger_error('Could not find the function [' . $this->taghandler["$name"] . '()] for the XML tag "' . $name . '"', E_USER_ERROR
);
187 $this->attribs
['value'] = $this->cdata
;
193 $this->detach_node();
197 * Shifts the node tree
199 * @param array Node to place into the stack
201 function attach_node(&$node)
204 $this->stack
[ count($this->stack
) ] =& $node;
206 // new attributes to work with
207 $this->attribs
=& $node;
211 * Unshifts the node tree
213 function detach_node()
215 // drop the newest node
216 unset($this->stack
[ count($this->stack
) - 1 ]);
218 // assign the attributes to the next newest node
219 $this->attribs
=& $this->stack
[ count($this->stack
) - 1 ];
223 /*=====================================================================*\
224 || ###################################################################
227 || ###################################################################
228 \*=====================================================================*/