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
61 $this->parser
= xml_parser_create();
62 xml_set_object($this->parser
, $this);
63 xml_parser_set_option($this->parser
, XML_OPTION_CASE_FOLDING
, 0);
64 xml_set_element_handler($this->parser
, '_tag_start', '_tag_end');
65 xml_set_character_data_handler($this->parser
, '_cdata');
67 if (!xml_parse($this->parser
, $data))
69 $error['code'] = xml_get_error_code($this->parser
);
70 $error['string'] = xml_error_string($error['code']);
71 $error['line'] = xml_get_current_line_number($this->parser
);
72 $error['column'] = xml_get_current_column_number($this->parser
);
73 print(sprintf("XML Error: %s (%d) at line %d colunn %d", $error['string'], $error['code'], $error['line'], $error['column']));
77 xml_parser_free($this->parser
);
83 * Process the opening location of an XML tag
85 * @param res XML parser
87 * @param array Tag attributes
89 function _tag_start($parser, $name, $attributes)
91 // we're opening a new tag
93 $this->tagname
= $name;
95 // copy all tag attributes
96 array_walk($attributes, 'trim');
97 $this->attrdata
[ $this->tagid
] = $attributes;
99 $this->parent
= current($this->tree
);
100 if ($this->parent
=== false
)
104 // advance the parent counter because the tree is yet to be updated
110 // initialize the data set
111 $this->taginfo
["$name"][ $this->parent
][ $this->tagid
] = '';
113 // add the current tag into the tree
114 $this->tree
[] = $this->tagid
;
120 * @param res XML parser
121 * @param str CDATA from tag
123 function _cdata($parser, $data)
128 /*if (preg_replace('#(^[[:space:]]+|[[:space:]]+$)#', '', trim($data)) == '')
131 $datanew = str_replace(array("\t", "\n", "\r", " "), array("{t}", "{n}", "{r}", "{s}"), $data);
132 $_isso->debug("strangechars[" . $this->tagid . "] = '$datanew'");
133 $data = $datanew . "||$count||";
137 $_isso->debug("cdata[] = '$data'");
139 if ($this->parent
== $this->tagid
)
141 if (!isset($this->taginfo
[ $this->tagname
][ $this->parent
- 1 ][ $this->tagid
]))
143 $this->taginfo
[ $this->tagname
][ $this->parent
- 1 ][ $this->tagid
] = '';
146 $this->taginfo
[ $this->tagname
][ $this->parent
- 1 ][ $this->tagid
] .= $data;
150 $this->taginfo
[ $this->tagname
][ $this->parent
][ $this->tagid
] .= $data;
155 * Process the closing of an XML tag
157 * @param res XML parser
158 * @param str Tag name
160 function _tag_end($parser, $name)
165 if (isset($this->taginfo
[ $this->tagname
][ $this->parent
][ $this->tagid
]))
167 $data = $this->taginfo
[ $this->tagname
][ $this->parent
][ $this->tagid
];
174 if (trim($data) == '')
176 $_isso->debug("not going to bother with '" . $this->tagid
. "'");
179 // if we have a data handler, operate it now
180 if (isset($this->taghandler
[ $this->tagname
]))
182 $_isso->debug("handler: {$this->taghandler[ " . $this->tagname
. " ]}");
183 if (function_exists($this->taghandler
[ $this->tagname
]))
185 $data = $this->taghandler
[ $this->tagname
]($data);
189 // check for necessary trims
192 // trim() doesn't take off the edge for more than a few white characters, preg_replace does; don't use [:space:] either...
193 //$data = preg_replace('#(^\s+|\s+$)#', '', $data);
194 //$data = preg_replace('#(^[[:space:]]+|[[:space:]]+$)#', '', trim($data));
195 $data = preg_replace('#^\s+#', '', $data);
196 $data = preg_replace('#\s+$#', '', $data);
197 $_isso->debug("trimming data ('" . $this->tagid
. "'): '" . $data . "'");
198 //$data = trim($data);
201 // don't want blank fields
202 if (trim($data) != '')
204 $this->taginfo
[ $this->tagname
][ $this->parent
][ $this->tagid
] = $data;
208 unset($this->taginfo
[ $this->tagname
][ $this->parent
][ $this->tagid
]);
211 $_isso->debug("tree[] = " . current($this->tree
));
213 // make sure that we're always at the end of the tree
218 /*=====================================================================*\
219 || ###################################################################
222 || ###################################################################
223 \*=====================================================================*/