-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxml2array_class.php
88 lines (76 loc) · 2.43 KB
/
xml2array_class.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
<?
class XmlToArray
{
var $xml='';
var $categories_relation;
function XmlToArray()
{
return true;
}
function _struct_to_array($values, &$i)
{
$child = array();
if (isset($values[$i]['value'])) array_push($child, $values[$i]['value']);
while ($i++ < count($values)) {
switch ($values[$i]['type']) {
case 'cdata':
array_push($child, $values[$i]['value']);
break;
case 'complete':
$name = $values[$i]['tag'];
if(!empty($name)){
if($name == 'category'){
$child[$name][$i] = ($values[$i]['value'])?($values[$i]['value']):'';
}
else{
$child[$name] = ($values[$i]['value'])?($values[$i]['value']):'';
}
if(isset($values[$i]['attributes'])) {
if($name == 'category'){
$child[$name][$i] = $values[$i]['attributes'];
}
elseif($name == 'item'){
$child[$name] = $values[$i]['attributes'];
}
}
}
break;
case 'open':
$name = $values[$i]['tag'];
$size = isset($child[$name]) ? sizeof($child[$name]) : 0;
$child[$name][$size] = $this->_struct_to_array($values, $i);
break;
case 'close':
return $child;
break;
}
}
return $child;
}
function createArray($xml,$type=false){
$this->xml = $xml;
$values = array();
$index = array();
$array = array();
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($parser, $xml, $values, $index);
xml_parser_free($parser);
$i = 0;
$name = $values[$i]['tag'];
$array[$name] = isset($values[$i]['attributes']) ? $values[$i]['attributes'] : '';
$array[$name] = $this->_struct_to_array($values, $i);
return $array;
}
function getXmlData($handle){
if ($handle) {
while (!feof($handle)) {
$xml_data.= fgets($handle, 4096);
}
fclose($handle);
}
return $xml_data;
}
}
?>