/
dev_xml.php
111 lines (97 loc) · 2.54 KB
/
dev_xml.php
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
//dev_xml.php
class DevXML {
var $filename;
var $parser;
var $data;
var $msg;
function DevXML($file = '') {
$this->parser = xml_parser_create();
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, true);
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, 'startHandler', 'endHandler');
xml_set_character_data_handler($this->parser, 'dataHandler');
if (dev_not_null($file)) {
$this->setFile($file);
$this->parseXML($file);
}
}
function setFile($file = '') {
if (dev_not_null($file)) {
$this->filename = $file;
}
}
function getFile() {
return $this->filename;
}
function parseXML($file = '') {
if (dev_is_null($file)) {
$file = $this->getFile();
}
if ($stream = dev_stream_file($file, $msg)) {
while ($data = fread($stream, 4096)) {
if (!xml_parse($this->parser, $data, feof($stream))) {
$this->setMsg(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser)));
return false;
}
}
} else {
$this->setMsg($msg);
return false;
}
return true;
}
function startHandler($parser, $name = '', $attributes = '') {
$data['name'] = $name;
if ($attributes) $data['attributes'] = $attributes;
$this->data[] = $data;
}
function dataHandler($parser, $data = '') {
if ($data = trim($data)) {
$index = count($this->data)-1;
$this->data[$index]['content'] .= $data;
}
}
function endHandler($parser, $name = '') {
if (count($this->data) > 1) {
$data = array_pop($this->data);
$index = count($this->data)-1;
$this->data[$index]['child'][] = $data;
}
}
function buildXML($data = '', $indent = 0) {
$xml = '';
$tabs = "";
for ($i=0; $i<$indent; $i++) $tabs .= "\t";
//if (!is_array($data)) $data = dev_value_to_array($data);
if (is_array($data)) {
foreach($data as $b=>$a) {
if (!dev_is_assoc($a)) {
$xml .= $this->buildXML($a, $indent);
} else {
$attribs = '';
if (dev_is_assoc($a['attributes'])) foreach($a['attributes'] as $c=>$d) $attribs .= " $c=\"$d\"";
$xml .= "$tabs<" . $a['name'] . "" . $attribs . ">" . ((count($a['child']) > 0) ? "\n" . $this->buildXML($a['child'], ++$indent) . "\n$tabs" : $a['content']) . "</" . $a['name'] . ">\n";
}
}
}
return $xml;
}
function setMsg($msg) {
$this->msg = $msg;
}
function getMsg() {
return $this->msg;
}
function getData() {
return $this->data;
}
function outputXML($data = '') {
header("Content-Type: XML");
$xml = 'No XML';
if ($data == '') $data = $this->data;
$xml = $this->buildXML($data);
echo $xml;
}
} //End class DevXML
?>