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.php51
1 files changed, 35 insertions, 16 deletions
diff --git a/code/xmlparser.inc.php b/code/xmlparser.inc.php
index 2d53a1f..0666c5a 100644
--- a/code/xmlparser.inc.php
+++ b/code/xmlparser.inc.php
@@ -1,27 +1,24 @@
<?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;
+ $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($this->parser, $xml, feof($file))) {
+ if(!xml_parse($parser, $xml, feof($file))) {
$this->tags = null;
break;
}
@@ -29,6 +26,8 @@
fclose($file);
+ xml_parser_free($parser);
+
return $this->tags[0];
}
@@ -36,9 +35,19 @@
$this->tags = array();
$this->depth = 0;
- if(!xml_parse($this->parser, $xml, true))
+ $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];
}
@@ -50,6 +59,10 @@
}
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());
@@ -58,6 +71,10 @@
}
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)
@@ -65,10 +82,12 @@
}
function cdataHandler($parser, $data) {
- $data = trim($data);
+ $children = &$this->tags[$this->depth-1]['children'];
- if($data != '')
- array_push($this->tags[$this->depth-1]['children'], $data);
+ if(is_string($children[count($children)-1]))
+ $children[count($children)-1] .= $data;
+ elseif(trim($data) != '')
+ array_push($children, $data);
}
}