1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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;
?>
|