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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
/**
* =============================================================
* The simple class for parse XML content to PHP array.
* Version : 1.0.1
* Last Upd : Thu Jan 30 14:51:18 GMT 2003
* Created By : Heiko Mundle [hmundle1@gmx.net]
* New Features: reads all attributes of a xml tag
* doesn't overwrite tags with same tag name (and same tag attribute)
*
* The simple class for parse XML content to PHP array.
* Version : 1.0
* Last Upd : Thu Oct 24 11:51:18 GMT 2002
* Created By : Phairote Charoen [phairotec@hotmail.com]
* =============================================================
* This XML content must specific some attribute for the key of record.
* <root>
* <item id="1" class="myclass">
* <name>xxxx</name>
* <price>100</price>
* </item>
* more content
* </root>
*
* Result :
*Array
*(
* [0] => Array
* (
* [name] => root
* [attr] => Array
* (
* )
* [cont] => Array
* (
* [0] => Array
* (
* [name] => item
* [attr] => Array
* (
* [id] => 1
* [class] => myclass
* )
* [cont] => Array
* (
* [0] => Array
* (
* [name] => name
* [attr] => Array
* (
* )
* [cont] => Array
* (
* [0] => xxxx
* )
* )
* [1] => Array
* (
* [name] => price
* [attr] => Array
* (
* )
* [cont] => Array
* (
* [0] => 100
* )
* )
* )
* )
* [1] => more content
* )
* )
*)
*
*/
define(WRITE_FLAG_NOT_NULL, 1);
define(WRITE_FLAG_NULL, 2);
define(WRITE_FLAG_EMPTY_ARRAY, 4);
class Xml2Array
{
/**
* The XML file name( can be also real path and web url ).
*/
var $xmlFile;
/**
* The array result from this class.
*/
var $arrResult = array();
/**
* The key stack, I keep each tag name of each level to stack array.
*/
var $keyStack = array();
/**
* Deep of stack array.
*/
var $stackIndex = 0;
/**
* The XML Parser ..
*/
var $parser;
/**
* The constructor.
* @xmlFile (string) - XML source file name
*/
function Xml2Array($xmlFile)
{
$this->xmlFile = $xmlFile;
$this->parser = xml_parser_create();
xml_set_object($this->parser, &$this);
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE,true);
xml_set_element_handler($this->parser, "startElement", "endElement");
xml_set_character_data_handler($this->parser, "characterData");
}
/**
* start element handler
*/
function startElement($parser, $name, $atts)
{
$name = trim($name);
$txt = "\$arr = " . $this->getArrayName($this->stackIndex) . ";";
eval($txt);
$this->keyStack[$this->stackIndex] = count($arr);
$this->stackIndex++;
$this->writeData("", WRITE_FLAG_EMPTY_ARRAY);
if(!empty($name))
{
// using the element name to array key..
$this->keyStack[$this->stackIndex] = "'name'";
$this->stackIndex++;
$this->writeData($name, WRITE_FLAG_NULL);
$this->stackIndex--;
$this->keyStack[$this->stackIndex] = "'attr'";
$this->stackIndex++;
if(!empty($atts))
{
// using the attributes to array key..
foreach($atts as $attKey => $attVal)
{
$this->keyStack[$this->stackIndex] = "'".$attKey."'";
$this->stackIndex++;
$this->writeData($attVal, WRITE_FLAG_NULL);
$this->stackIndex--;
}
}
else
{
$this->writeData("", WRITE_FLAG_EMPTY_ARRAY);
}
$this->stackIndex--;
$this->keyStack[$this->stackIndex] = "'cont'";
$this->stackIndex++;
}
}
/**
* end element handler
*/
function endElement($parser, $name)
{
$this->stackIndex-=2;
}
/**
* none tag element handler
*/
function characterData($parser, $data)
{
$data = trim($data);
if(!empty($data))
{
$txt = "\$arr = " . $this->getArrayName($this->stackIndex) . ";";
eval($txt);
$this->keyStack[$this->stackIndex] = count($arr);
$this->stackIndex++;
$this->writeData($data, WRITE_FLAG_NOT_NULL);
$this->stackIndex--;
}
}
/**
* write data in array
*/
function writeData($data, $flag)
{
$data = trim($data);
$dataStr = "\"" . $data . "\"";
switch( $flag )
{
case WRITE_FLAG_NOT_NULL:
default:
// stop if there isn't any data
if(empty($data))
break;
case WRITE_FLAG_EMPTY_ARRAY:
// overwrite default dataStr
if(empty($data))
$dataStr = "array()";
case WRITE_FLAG_NULL:
$txt = $this->getArrayName($this->stackIndex)." = ".$dataStr.";";
// assign value to the result array ..
eval($txt);
break;
}
}
/**
* get name of array
*/
function getArrayName($depth)
{
if($depth)
{
for($i=0; $i<$depth; $i++)
{
$key .= $this->keyStack[$i]."|";
}
$key = preg_replace("/\|$/", "]", $key);
$key = preg_replace("/\|/", "][", $key);
return "\$this->arrResult[".$key;
}
else
{
return "\$this->arrResult";
}
}
/**
* parsing ..
*/
function parse()
{
if (!($fp = fopen($this->xmlFile, "r")))
{
die("Could not open XML input!");
}
while ($data = fread($fp, 4096))
{
if (!xml_parse($this->parser, $data, feof($fp)))
{
die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)),xml_get_current_line_number($this->parser)));
}
}
fclose($fp);
}
/**
* @return the result array
*/
function getResult()
{
return $this->arrResult;
}
/**
* free the XML parser result ..
*/
function freeResult()
{
xml_parser_free($this->parser);
}
} // end class
?>
<?php
// Testing the clsXml2Array class.
$xmlFile = "test.xml";
$r = new clsXml2Array($xmlFile);
$r->parse();
$arrResult = $r->getResult();
$r->freeResult();
echo "<PRE>";
print_r($arrResult);
echo "</PRE>";
?>