]>
src.bluestatic.org Git - isso.git/blob - xml.php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # All parts of this file are ©2003-[#]year[#] Iris Studios, Inc. No # ||
7 || # part of this file may be reproduced in any way: part or whole. # ||
8 || # --------------------------------------------------------------- # ||
9 || # ©2003 - [#]year[#] Iris Studios, Inc. | http://www.iris-studios.com # ||
10 || ################################################################### ||
11 \*=====================================================================*/
13 $OBJECT = 'XML Parser';
14 $CLASS = 'XML_Parser';
20 * This framework is a wrapper for a robust XML parser.
22 * @author Iris Studios, Inc.
23 * @copyright Copyright ©2003 - [#]year[#], Iris Studios, Inc.
30 * Global environment variables
32 * @var parser Current XML parser
33 * @var taginfo Array of all the processed tag data
34 * @var taghandler What to do for each tag encountered
35 * @var trimdata Trim CDATA elements?
36 * @var attrdata Data parsed from global attributes
37 * @var tagid Current tag ID in the document
38 * @var tagname Current tag name in the document
39 * @var tree Tag stack to use for proper nesting
40 * @var parent The tag's parent
43 var $taginfo = array();
44 var $taghandler = array();
46 var $attrdata = array();
55 * @param str XML file data
57 * @return array Array with all the XML data parsed
63 // reset all the parser data before starting
64 $this->taginfo
= array();
65 $this->attrdata
= array();
68 $this->tree
= array();
71 // create a new parser
72 $this->parser
= xml_parser_create();
73 xml_set_object($this->parser
, $this);
74 xml_parser_set_option($this->parser
, XML_OPTION_CASE_FOLDING
, 0);
75 xml_set_element_handler($this->parser
, '_tag_start', '_tag_end');
76 xml_set_character_data_handler($this->parser
, '_cdata');
78 // parse the data and check for errors
79 if (!xml_parse($this->parser
, $data))
81 $error['code'] = xml_get_error_code($this->parser
);
82 $error['string'] = xml_error_string($error['code']);
83 $error['line'] = xml_get_current_line_number($this->parser
);
84 $error['column'] = xml_get_current_column_number($this->parser
);
85 print(sprintf("XML Error: %s (%d) at line %d colunn %d", $error['string'], $error['code'], $error['line'], $error['column']));
90 xml_parser_free($this->parser
);
92 // data manipulation -- kind of resource intensive :-/
93 foreach ($this->taginfo
AS $tagname => $parents)
95 foreach ($parents AS $parentid => $ids)
97 foreach ($ids AS $tagid => $data)
99 // trim data if necessary
102 $_isso->debug("data trim: on");
103 if (($trimmed = trim($data)) != '')
109 // if we have a data handler, operate it now
110 if (isset($this->taghandler
["$tagname"]))
112 $_isso->debug("handler
: " . $this->taghandler["$tagname"]);
113 if (function_exists($this->taghandler
["$tagname"]))
115 $data = $this->taghandler["$tagname"]($data, $this);
119 trigger_error('Could not find the function [' . $this->taghandler
["$tagname"] . '()] for the XML tag "' . $tagname . '"', ERR_FATAL);
123 $return["$tagid"] = array('tagid' => $tagid, 'tagname' => $tagname, 'parentid' => $parentid, 'attributes' => $this->attrdata
["$tagid"], 'data' => $data);
128 // done... send the results back
133 * Process the opening location of an XML tag
135 * @param res XML parser
136 * @param str Tag name
137 * @param array Tag attributes
139 function _tag_start($parser, $name, $attributes)
141 // we're opening a new tag
143 $this->tagname = $name;
145 // copy all tag attributes
146 array_walk($attributes, 'trim');
147 $this->attrdata[ $this->tagid ] = $attributes;
149 $this->parent = current($this->tree);
150 if ($this->parent === false)
154 // advance the parent counter because the tree is yet to be updated
160 // initialize the data set
161 $this->taginfo["$name"][ $this->parent
][ $this->tagid
] = '';
163 // add the current tag into the tree
164 $this->tree
[] = $this->tagid
;
170 * @param res XML parser
171 * @param str CDATA from tag
173 function _cdata($parser, $data)
176 if ($this->parent
== $this->tagid
)
178 if (!isset($this->taginfo
[ $this->tagname
][ $this->parent
][ $this->tagid
]))
180 $this->taginfo
[ $this->tagname
][ $this->parent
][ $this->tagid
] = '';
183 $this->taginfo
[ $this->tagname
][ $this->parent
][ $this->tagid
] .= $data;
187 $this->taginfo
[ $this->tagname
][ $this->parent
][ $this->tagid
] .= $data;
192 * Process the closing of an XML tag
194 * @param res XML parser
195 * @param str Tag name
197 function _tag_end($parser, $name)
199 // make sure that we're always at the end of the tree
204 /*=====================================================================*\
205 || ###################################################################
208 || ###################################################################
209 \*=====================================================================*/