BMLT Root Server
xml_utils.php
Go to the documentation of this file.
1 <?php
2 defined('BMLT_EXEC') or die('Cannot Execute Directly'); // Makes sure that this file is in the correct context.
3 
4 /*
5  Working with XML. Usage:
6  $xml=xml2ary(file_get_contents('1.xml'));
7  $link=$xml['ddd']['_c'];
8  $link['twomore']=$link['onemore'];
9  // ins2ary(); // dot not insert a link, and arrays with links inside!
10  echo ary2xml($xml);
11 
12  This file is part of the Basic Meeting List Toolbox (BMLT).
13 
14  Find out more at: https://bmlt.app
15 
16  BMLT is free software: you can redistribute it and/or modify
17  it under the terms of the MIT License.
18 
19  BMLT is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  MIT License for more details.
23 
24  You should have received a copy of the MIT License along with this code.
25  If not, see <https://opensource.org/licenses/MIT>.
26 */
27 
28 // XML to Array
29 function xml2ary(&$string)
30 {
31  $parser = xml_parser_create();
32  xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
33  xml_parse_into_struct($parser, $string, $vals, $index);
34  xml_parser_free($parser);
35 
36  $mnary=array();
37  $ary=$mnary;
38  foreach ($vals as $r) {
39  $t=$r['tag'];
40  if ($r['type']=='open') {
41  if (isset($ary[$t])) {
42  if (isset($ary[$t][0])) {
43  $ary[$t][]=array();
44  } else {
45  $ary[$t]=array($ary[$t], array());
46  }
47  $cv=$ary[$t][count($ary[$t])-1];
48  } else {
49  $cv=$ary[$t];
50  }
51  if (isset($r['attributes'])) {
52  foreach ($r['attributes'] as $k => $v) {
53  $cv['_a'][$k]=$v;
54  }
55  }
56  $cv['_c']=array();
57  $cv['_c']['_p']=$ary;
58  $ary=$cv['_c'];
59  } elseif ($r['type']=='complete') {
60  if (isset($ary[$t])) { // same as open
61  if (isset($ary[$t][0])) {
62  $ary[$t][]=array();
63  } else {
64  $ary[$t]=array($ary[$t], array());
65  }
66  $cv=$ary[$t][count($ary[$t])-1];
67  } else {
68  $cv=$ary[$t];
69  }
70  if (isset($r['attributes'])) {
71  foreach ($r['attributes'] as $k => $v) {
72  $cv['_a'][$k]=$v;
73  }
74  }
75  $cv['_v']=(isset($r['value']) ? $r['value'] : '');
76  } elseif ($r['type']=='close') {
77  $ary=$ary['_p'];
78  }
79  }
80 
81  _del_p($mnary);
82  return $mnary;
83 }
84 
85 // _Internal: Remove recursion in result array
86 function _del_p(&$ary)
87 {
88  foreach ($ary as $k => $v) {
89  if ($k==='_p') {
90  unset($ary[$k]);
91  } elseif (is_array($ary[$k])) {
92  _del_p($ary[$k]);
93  }
94  }
95 }
96 
97 // Array to XML
98 function ary2xml($cary, $d = 0, $forcetag = '')
99 {
100  $res=array();
101  foreach ($cary as $tag => $r) {
102  if (isset($r[0])) {
103  $res[]=ary2xml($r, $d, $tag);
104  } else {
105  if ($forcetag) {
106  $tag=$forcetag;
107  }
108  $sp=str_repeat("\t", $d);
109  $res[]="$sp<$tag";
110  if (isset($r['_a'])) {
111  foreach ($r['_a'] as $at => $av) {
112  $res[]=" $at=\"$av\"";
113  }
114  }
115  $res[]=">".((isset($r['_c'])) ? "\n" : '');
116  if (isset($r['_c'])) {
117  $res[]=ary2xml($r['_c'], $d+1);
118  } elseif (isset($r['_v'])) {
119  $res[]=$r['_v'];
120  }
121  $res[]=(isset($r['_c']) ? $sp : '')."</$tag>\n";
122  }
123  }
124  return implode('', $res);
125 }
126 
127 // Insert element into array
128 function ins2ary(&$ary, $element, $pos)
129 {
130  $ar1=array_slice($ary, 0, $pos);
131  $ar1[]=$element;
132  $ary=array_merge($ar1, array_slice($ary, $pos));
133 }
ary2xml($cary, $d=0, $forcetag= '')
Definition: xml_utils.php:98
_del_p(&$ary)
Definition: xml_utils.php:86
xml2ary(&$string)
Definition: xml_utils.php:29
defined('BMLT_EXEC') or define('BMLT_EXEC'
Definition: index.php:3
ins2ary(&$ary, $element, $pos)
Definition: xml_utils.php:128