diff options
Diffstat (limited to 'core/xmlparser.inc.php')
-rw-r--r-- | core/xmlparser.inc.php | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/core/xmlparser.inc.php b/core/xmlparser.inc.php new file mode 100644 index 0000000..0666c5a --- /dev/null +++ b/core/xmlparser.inc.php @@ -0,0 +1,95 @@ +<?PHP + class XMLParser { + var $tags; + var $depth; + + function ParseFile($filename) { + $this->tags = array(); + $this->depth = 0; + + $parser = xml_parser_create(); + + xml_set_object($parser, $this); + xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); + + xml_set_element_handler($parser, 'openTagHandler', 'closeTagHandler'); + xml_set_character_data_handler($parser, 'cdataHandler'); + + $file = fopen($filename, 'r'); + + while ($xml = fread($file, 4096)) { + if(!xml_parse($parser, $xml, feof($file))) { + $this->tags = null; + break; + } + } + + fclose($file); + + xml_parser_free($parser); + + return $this->tags[0]; + } + + function ParseXML($xml) { + $this->tags = array(); + $this->depth = 0; + + $parser = xml_parser_create(); + + xml_set_object($parser, $this); + xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); + + xml_set_element_handler($parser, 'openTagHandler', 'closeTagHandler'); + xml_set_character_data_handler($parser, 'cdataHandler'); + + if(!xml_parse($parser, $xml, true)) + $this->tags = null; + + xml_parser_free($parser); + + 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) { + $children = &$this->tags[$this->depth-1]['children']; + + $children[count($children)-1] = trim($children[count($children)-1]); + + $this->tags[$this->depth] = array('tag' => $name, + 'attribs' => $attribs, + 'children' => array()); + + $this->depth++; + } + + function closeTagHandler($parser, $name) { + $children = &$this->tags[$this->depth-1]['children']; + + $children[count($children)-1] = trim($children[count($children)-1]); + + $this->depth--; + + if($this->depth > 0) + array_push($this->tags[$this->depth-1]['children'], $this->tags[$this->depth]); + } + + function cdataHandler($parser, $data) { + $children = &$this->tags[$this->depth-1]['children']; + + if(is_string($children[count($children)-1])) + $children[count($children)-1] .= $data; + elseif(trim($data) != '') + array_push($children, $data); + } + } + + $GLOBALS['xmlparser'] = new XMLParser; +?> |