1
|
|
|
<?php |
2
|
|
|
/*************************************************************************** |
3
|
|
|
* for license information see LICENSE.md |
4
|
|
|
***************************************************************************/ |
5
|
|
|
|
6
|
|
|
class xml2Array |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
public $stack = []; |
9
|
|
|
public $stack_ref; |
10
|
|
|
public $arrOutput = []; |
11
|
|
|
public $resParser; |
12
|
|
|
public $strXmlData; |
13
|
|
|
|
14
|
|
|
public function push_pos(&$pos) |
15
|
|
|
{ |
16
|
|
|
$this->stack[count($this->stack)] =& $pos; |
17
|
|
|
$this->stack_ref =& $pos; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function pop_pos() |
21
|
|
|
{ |
22
|
|
|
unset($this->stack[count($this->stack) - 1]); |
23
|
|
|
$this->stack_ref =& $this->stack[count($this->stack) - 1]; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param false|string $strInputXML |
28
|
|
|
*/ |
29
|
|
|
public function parse($strInputXML) |
30
|
|
|
{ |
31
|
|
|
$this->resParser = xml_parser_create('UTF-8'); |
32
|
|
|
xml_set_object($this->resParser, $this); |
33
|
|
|
xml_set_element_handler($this->resParser, 'tagOpen', 'tagClosed'); |
34
|
|
|
|
35
|
|
|
xml_set_character_data_handler($this->resParser, 'tagData'); |
36
|
|
|
|
37
|
|
|
$this->push_pos($this->arrOutput); |
38
|
|
|
|
39
|
|
|
$this->strXmlData = xml_parse($this->resParser, $strInputXML); |
40
|
|
|
if (!$this->strXmlData) { |
41
|
|
|
die(sprintf( |
|
|
|
|
42
|
|
|
'XML error: %s at line %d', |
43
|
|
|
xml_error_string(xml_get_error_code($this->resParser)), |
44
|
|
|
xml_get_current_line_number($this->resParser) |
45
|
|
|
)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
xml_parser_free($this->resParser); |
49
|
|
|
|
50
|
|
|
return $this->arrOutput; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function tagOpen($parser, $name, $attrs) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
if (isset($this->stack_ref[$name])) { |
56
|
|
|
if (!isset($this->stack_ref[$name][0])) { |
57
|
|
|
$tmp = $this->stack_ref[$name]; |
58
|
|
|
unset($this->stack_ref[$name]); |
59
|
|
|
$this->stack_ref[$name][0] = $tmp; |
60
|
|
|
} |
61
|
|
|
$cnt = count($this->stack_ref[$name]); |
62
|
|
|
$this->stack_ref[$name][$cnt] = []; |
63
|
|
|
if (isset($attrs)) { |
64
|
|
|
$this->stack_ref[$name][$cnt] = $attrs; |
65
|
|
|
} |
66
|
|
|
$this->push_pos($this->stack_ref[$name][$cnt]); |
67
|
|
|
} else { |
68
|
|
|
$this->stack_ref[$name] = []; |
69
|
|
|
if (isset($attrs)) { |
70
|
|
|
$this->stack_ref[$name] = $attrs; |
71
|
|
|
} |
72
|
|
|
$this->push_pos($this->stack_ref[$name]); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function tagData($parser, $tagData) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
if (mb_trim($tagData)) { |
79
|
|
|
if (isset($this->stack_ref['DATA'])) { |
80
|
|
|
$this->stack_ref['DATA'] .= $tagData; |
81
|
|
|
} else { |
82
|
|
|
$this->stack_ref['DATA'] = $tagData; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function tagClosed($parser, $name) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$this->pop_pos(); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.