]>
src.bluestatic.org Git - isso.git/blob - xml.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
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 \*=====================================================================*/
23 * Static XML parser (XML.php)
31 * A simple XML parser that is run by calling BSXml::Parse( String xmlData) and an
32 * array is returned with the parsed information.
35 * @copyright Copyright ©2002 - [#]year[#], Blue Static
46 private $parser = null;
49 * An array of function names that are to be executed for each tag name (name => function)
52 private $taghandler = array();
61 * Tag stack of all open nodes
64 private $stack = array();
67 * Node list for all open tag attributes
70 private $attribs = array();
73 * Resulting parsed array
76 private $result = array();
78 // ###################################################################
82 private function __construct() {}
84 // ###################################################################
88 * @param string XML file data
89 * @param string Parse file as UTF-8 instead of ISSO-8859-1
91 * @return array Array with all the XML data parsed
93 public static function Parse($data, $utf8 = false)
95 $parser = new BSXml();
99 $data = utf8_encode($data);
100 $parser->parser
= xml_parser_create('UTF-8');
104 $parser->parser
= xml_parser_create('ISO-8859-1');
107 // create a new parser
108 xml_set_object($parser->parser
, $parser);
109 xml_parser_set_option($parser->parser
, XML_OPTION_CASE_FOLDING
, 0);
110 xml_set_element_handler($parser->parser
, '_handleStartTag', '_handleEndTag');
111 xml_set_character_data_handler($parser->parser
, '_handleCData');
113 $parser->_attachNode($parser->result
);
115 // parse the data and check for errors
116 if (!xml_parse($parser->parser
, $data))
118 $error['code'] = xml_get_error_code($parser->parser
);
119 $error['string'] = xml_error_string($error['code']);
120 $error['line'] = xml_get_current_line_number($parser->parser
);
121 $error['column'] = xml_get_current_column_number($parser->parser
);
122 trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]");
126 // destroy the parser
127 xml_parser_free($parser->parser
);
129 // done... send the results back
130 return $parser->result
;
133 // ###################################################################
135 * Process the opening location of an XML tag
137 * @param integer XML parser
138 * @param string Tag name
139 * @param array Tag attributes
141 private function _handleStartTag(&$parser, $name, $attrs)
143 // we need to keep track of indicies to monitor the last key in $this->attribs
147 array_walk($attrs, 'trim');
150 if (isset($this->attribs
["$name"]))
152 // node set has < 1 child nodes
153 if (!isset($this->attribs["$name"][0]))
155 $tmp = $this->attribs
["$name"];
156 unset($this->attribs["$name"]);
157 $this->attribs
["$name"][0] = $tmp;
160 // create a new child node
161 $this->attribs["$name"][ $index["$name"] ] = (array)$attrs;
162 $this->_attachNode($this->attribs["$name"][ $index["$name"] ]);
165 // node set doesn't exist, so create it
168 $this->attribs
["$name"] = (array)$attrs;
169 $this->_attachNode($this->attribs["$name"]);
174 // ###################################################################
178 * @param integer XML parser
179 * @param string CDATA from tag
181 private function _handleCData(&$parser, $data)
183 $this->cdata .= $data;
186 // ###################################################################
188 * Process the closing of an XML tag
190 * @param integer XML parser
191 * @param string Tag name
193 private function _handleEndTag(&$parser, $name)
195 // attach data to the node
196 if (($this->cdata = trim($this->cdata)) != '')
198 $this->attribs['value'] = $this->cdata;
204 $this->_detachNode();
207 // ###################################################################
209 * Shifts the node tree
211 * @param array Node to place into the stack
213 private function _attachNode(&$node)
216 $this->stack[ sizeof($this->stack) ] =& $node;
218 // new attributes to work with
219 $this->attribs =& $node;
222 // ###################################################################
224 * Unshifts the node tree
226 private function _detachNode()
228 // drop the newest node
229 unset($this->stack[ sizeof($this->stack) - 1 ]);
231 // assign the attributes to the next newest node
232 $this->attribs =& $this->stack[ sizeof($this->stack) - 1 ];
235 // ###################################################################
237 * Unless a node has multiple children, there will not be a numerical
238 * index for the child node. So this means that if you have variable
239 * XML structure with some of the same types of nodes having one children
240 * or multiple children, you'd have different things to parse. If
241 * you want the node unified (as in, all single-children would be indexed
242 * numerically, run this function on the node. It works on references.
244 * @param array The node to int-index
246 public static function UnifyNode(&$node)
248 if (!isset($node[0]))
250 $node = array($node);
255 /*=====================================================================*\
256 || ###################################################################
259 || ###################################################################
260 \*=====================================================================*/