43 static function xml2array ($contents, $get_attributes = 1, $priority =
'tag')
49 if (!function_exists(
'xml_parser_create')) {
50 trigger_error(
'xml_parser_create function does not exists');
55 $parser = xml_parser_create(
'');
56 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING,
"UTF-8");
57 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
58 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
59 xml_parse_into_struct($parser, trim($contents), $xml_values);
60 xml_parser_free($parser);
70 $current = &$xml_array;
74 $repeated_tag_index = [];
75 foreach ($xml_values as $data) {
77 $attributes_data = [];
79 if (isset($data[
'value'])) {
80 if ($priority ==
'tag') {
81 $result = $data[
'value'];
84 $result[
'value'] = $data[
'value'];
89 if (isset($data[
'attributes']) and $get_attributes) {
90 foreach ($data[
'attributes'] as $attr => $val) {
91 if ($priority ==
'tag') {
92 $attributes_data[$attr] = $val;
95 $result[
'attr'][$attr] = $val;
101 if ($data[
'type'] ==
'open') {
103 $parent[$data[
'level'] - 1] = &$current;
104 if (!is_array($current) or (!in_array($data[
'tag'], array_keys($current)))) {
106 $current[$data[
'tag']] = $result;
107 if ($attributes_data) {
108 $current[$data[
'tag'].
'_attr'] = $attributes_data;
110 $repeated_tag_index[$data[
'tag'].
'_'.$data[
'level']] = 1;
112 $current = &$current[$data[
'tag']];
116 if (isset($current[$data[
'tag']][0])) {
118 $current[$data[
'tag']][$repeated_tag_index[$data[
'tag'].
'_'.$data[
'level']]] = $result;
119 $repeated_tag_index[$data[
'tag'].
'_'.$data[
'level']]++;
123 $current[$data[
'tag']] = [$current[$data[
'tag']],$result];
124 $repeated_tag_index[$data[
'tag'].
'_'.$data[
'level']] = 2;
126 if (isset($current[$data[
'tag'].
'_attr'])) {
128 $current[$data[
'tag']][
'0_attr'] = $current[$data[
'tag'].
'_attr'];
129 unset($current[$data[
'tag'].
'_attr']);
132 $last_item_index = $repeated_tag_index[$data[
'tag'].
'_'.$data[
'level']] - 1;
133 $current = &$current[$data[
'tag']][$last_item_index];
135 } elseif ($data[
'type'] ==
"complete") {
138 if (!isset($current[$data[
'tag']])) {
140 $current[$data[
'tag']] = $result;
141 $repeated_tag_index[$data[
'tag'].
'_'.$data[
'level']] = 1;
142 if ($priority ==
'tag' and $attributes_data) {
143 $current[$data[
'tag'].
'_attr'] = $attributes_data;
147 if (isset($current[$data[
'tag']][0]) and is_array($current[$data[
'tag']])) {
150 $current[$data[
'tag']][$repeated_tag_index[$data[
'tag'].
'_'.$data[
'level']]] = $result;
152 if ($priority ==
'tag' and $get_attributes and $attributes_data) {
153 $current[$data[
'tag']][$repeated_tag_index[$data[
'tag'].
'_'.$data[
'level']] .
'_attr'] = $attributes_data;
155 $repeated_tag_index[$data[
'tag'].
'_'.$data[
'level']]++;
158 $current[$data[
'tag']] = [$current[$data[
'tag']],$result];
159 $repeated_tag_index[$data[
'tag'].
'_'.$data[
'level']] = 1;
160 if ($priority ==
'tag' and $get_attributes) {
161 if (isset($current[$data[
'tag'].
'_attr'])) {
163 $current[$data[
'tag']][
'0_attr'] = $current[$data[
'tag'].
'_attr'];
164 unset($current[$data[
'tag'].
'_attr']);
167 if ($attributes_data) {
168 $current[$data[
'tag']][$repeated_tag_index[$data[
'tag'].
'_'.$data[
'level']] .
'_attr'] = $attributes_data;
172 $repeated_tag_index[$data[
'tag'].
'_'.$data[
'level']]++;
175 } elseif ($data[
'type'] ==
'close') {
177 $current = &$parent[$data[
'level'] - 1];
static xml2array($contents, $get_attributes=1, $priority='tag')
Transform a xml document to an array.
This class contains all the function needed to manage xml files.