summaryrefslogtreecommitdiffstats
path: root/code/xmlparser.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'code/xmlparser.inc.php')
-rw-r--r--code/xmlparser.inc.php76
1 files changed, 76 insertions, 0 deletions
diff --git a/code/xmlparser.inc.php b/code/xmlparser.inc.php
new file mode 100644
index 0000000..2d53a1f
--- /dev/null
+++ b/code/xmlparser.inc.php
@@ -0,0 +1,76 @@
+<?PHP
+ class XMLParser {
+ var $parser;
+ var $tags;
+ var $depth;
+
+ function XMLParser() {
+ $this->parser = xml_parser_create();
+
+ xml_set_object($this->parser, $this);
+ xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
+
+ xml_set_element_handler($this->parser, 'openTagHandler', 'closeTagHandler');
+ xml_set_character_data_handler($this->parser, 'cdataHandler');
+ }
+
+ function ParseFile($filename) {
+ $this->tags = array();
+ $this->depth = 0;
+
+ $file = fopen($filename, 'r');
+
+ while ($xml = fread($file, 4096)) {
+ if(!xml_parse($this->parser, $xml, feof($file))) {
+ $this->tags = null;
+ break;
+ }
+ }
+
+ fclose($file);
+
+ return $this->tags[0];
+ }
+
+ function ParseXML($xml) {
+ $this->tags = array();
+ $this->depth = 0;
+
+ if(!xml_parse($this->parser, $xml, true))
+ $this->tags = null;
+
+ return $this->tags[0];
+ }
+
+ function FindTag($data, $tag, $start = 0) {
+ for($i = $start; $i < count($data['children']); $i++) {
+ if(!is_array($data['children'][$i])) continue;
+ if($data['children'][$i]['tag'] == $tag) return $data['children'][$i];
+ }
+ }
+
+ function openTagHandler($parser, $name, $attribs) {
+ $this->tags[$this->depth] = array('tag' => $name,
+ 'attribs' => $attribs,
+ 'children' => array());
+
+ $this->depth++;
+ }
+
+ function closeTagHandler($parser, $name) {
+ $this->depth--;
+
+ if($this->depth > 0)
+ array_push($this->tags[$this->depth-1]['children'], $this->tags[$this->depth]);
+ }
+
+ function cdataHandler($parser, $data) {
+ $data = trim($data);
+
+ if($data != '')
+ array_push($this->tags[$this->depth-1]['children'], $data);
+ }
+ }
+
+ $GLOBALS['xmlparser'] = new XMLParser;
+?>