This repository has been archived by the owner on Apr 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml.php
236 lines (197 loc) · 7.31 KB
/
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/*
* Written by Aaron Colflesh: [email protected]
* Released under the LGPL License: http://www.gnu.org/licenses/lgpl.html
*
* Modified by Dan Coulter: [email protected]
* This version was modified to replace the built in PHP XML Parsing functions.
*/
/** Updated with phpFlickr 1.6 **/
require_once("xml_saxy_parser.php");
class SAXY_Custom {
public $result;
public $sp;
function SAXY_Custom() {
$this->sp = new SAXY_Parser();
$this->result = array();
$this->sp->xml_set_element_handler(array(&$this, "startElement"), array(&$this, "endElement"));
$this->sp->xml_set_character_data_handler(array(&$this, "charData"));
}
function parse($xml)
{
$this->sp->parse($xml);
return $this->sp->endElementHandler[0]->result;
}
function startElement($parser, $name, $attributes) {
$this->level++;
$tmp = array();
$tmp['tag'] = $name;
$tmp['type'] = 'open';
$tmp['level'] = $this->level;
$tmp['attributes'] = $attributes;
$this->result[] = $tmp;
}
function endElement($parser, $name) {
$tmp = array_pop($this->result);
if ($tmp['type'] == 'complete' && $tmp['tag'] == $name) {
$this->result[] = $tmp;
} elseif ($tmp['type'] == 'open' && $tmp['tag'] == $name) {
$tmp['type'] = 'complete';
$this->result[] = $tmp;
} else {
$this->result[] = $tmp;
$tmp = array();
$tmp['type'] = 'close';
$tmp['tag'] = $name;
$tmp['level'] = $this->level;
$this->result[] = $tmp;
}
$this->level--;
}
function charData($parser, $text) {
$tmp = array_pop($this->result);
$tmp['type'] = 'complete';
$tmp['value'] = $text;
$this->result[] = $tmp;
}
}
class xml {
/** If attributesDirectlyUnderParent is true then a tag's attributes will be merged into
* the tag itself rather than under the special '_attributes' key.
* For example:
* false: $tag['_attributes'][$attributeName];
* true: $tag[$attributeName]; OR $tag['_attributes'][$attributeName];
*
* @var boolean
*/
public $attributesDirectlyUnderParent = false;
/** If childTagsDirectlyUnderParent is true then a tag's children will be merged into
* the tag itself rather than under the special '_value' key.
* For example:
* false: $tag['_value'][$childTagName];
* true: $tag[$childTagName];
*
* @var boolean
*/
public $childTagsDirectlyUnderParent = false;
public $caseInsensitive = false;
//public $_replace = array('�','&',"\n","", "�","�");
//public $_replaceWith = array('{deg}', '{amp}', '{lf}','{ESC}', "Â","{GBP}");
public $_replace = array();
public $_replaceWith = array();
function xml($caseInsensitive = false, $attributesDirectlyUnderParent = false, $childTagsDirectlyUnderParent = false)
{
$this->caseInsensitive = $caseInsensitive;
$this->attributesDirectlyUnderParent = $attributesDirectlyUnderParent;
$this->childTagsDirectlyUnderParent = $childTagsDirectlyUnderParent;
}
function parse($xml)
{
/* This is the original code that uses PHP's crappy XML functions.
$this->_parser = xml_parser_create();
$this->input = $xml;
$xml = str_replace($this->_replace, $this->_replaceWith, $xml);
$xml = str_replace(">{lf}", ">\n", $xml);
unset($this->_struct, $this->_index, $this->parsed);
xml_set_object($this->_parser, $this);
xml_parser_set_option($this->_parser, XML_OPTION_CASE_FOLDING, $this->caseInsensitive);
xml_parser_set_option($this->_parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($this->_parser, $xml, $this->_struct, $this->_index);
//*/
//* This is the replacement code that uses the SAXY Parser class I defined above.
$this->_parser = new SAXY_Custom;
$this->_struct = $this->_parser->parse($xml);
//*/
$this->parsed = $this->_postProcess($this->_struct);
$this->parsed = array($this->parsed['_name']=>$this->parsed);
return $this->parsed;
}
/* You'll note that I used php's array pointer functions in the _postProcess function.
In fact it looks like I made a foreach overly complicated in the 'open' case of the
switch statement. However this is not the case. By using the array pointer functions,
each time you go another call deeper (or shallower) in the recursion it doesn't loose
its place in the structure array.*/
function _postProcess() {
$item = current($this->_struct);
$ret = array('_name'=>$item['tag'], '_attributes'=>array(), '_value'=>null);
if (isset($item['attributes']) && count($item['attributes'])>0) {
foreach ($item['attributes'] as $key => $data) {
if (!is_null($data)) {
$item['attributes'][$key] = str_replace($this->_replaceWith, $this->_replace, $item['attributes'][$key]);
}
}
$ret['_attributes'] = $item['attributes'];
if ($this->attributesDirectlyUnderParent)
$ret = array_merge($ret, $item['attributes']);
}
if (isset($item['value']) && $item['value'] != null)
$item['value'] = str_replace($this->_replaceWith, $this->_replace, $item['value']);
switch ($item['type']) {
case 'open':
$children = array();
while (($child = next($this->_struct)) !== FALSE ) {
if ($child['level'] <= $item['level'])
break;
$subItem = $this->_postProcess();
if (isset($subItem['_name'])) {
if (!isset($children[$subItem['_name']]))
$children[$subItem['_name']] = array();
$children[$subItem['_name']][] = $subItem;
}
else {
foreach ($subItem as $key=>$value) {
if (isset($children[$key])) {
if (is_array($children[$key]))
$children[$key][] = $value;
else
$children[$key] = array($children[$key], $value);
}
else {
$children[$key] = $value;
}
}
}
}
if ($this->childTagsDirectlyUnderParent)
$ret = array_merge($ret, $this->_condenseArray($children));
else
$ret['_value'] = $this->_condenseArray($children);
break;
case 'close':
break;
case 'complete':
if (count($ret['_attributes']) > 0) {
if (isset($item['value']))
$ret['_value'] = $item['value'];
}
else {
if (isset($item['value'])) {
$ret = array($item['tag']=> $item['value']);
} else {
$ret = array($item['tag']=> "");
}
}
break;
}
//added by Dan Coulter
/*
foreach ($ret as $key => $data) {
if (!is_null($data) && !is_array($data)) {
$ret[$key] = str_replace($this->_replaceWith, $this->_replace, $ret[$key]);
}
}
*/
return $ret;
}
function _condenseArray($array) {
$newArray = array();
foreach ($array as $key => $value) {
if (is_array($value) && count($value)==1)
$newArray[$key] = current($value);
else
$newArray[$key] = $value;
}
return $newArray;
}
}
?>