BMLT Root Server
Functions
xml_utils.php File Reference

Go to the source code of this file.

Functions

 xml2ary (&$string)
 
 _del_p (&$ary)
 
 ary2xml ($cary, $d=0, $forcetag= '')
 
 ins2ary (&$ary, $element, $pos)
 

Function Documentation

_del_p ( $ary)

Definition at line 86 of file xml_utils.php.

Referenced by xml2ary().

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 }
_del_p(&$ary)
Definition: xml_utils.php:86
ary2xml (   $cary,
  $d = 0,
  $forcetag = '' 
)

Definition at line 98 of file xml_utils.php.

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 }
ary2xml($cary, $d=0, $forcetag= '')
Definition: xml_utils.php:98
ins2ary ( $ary,
  $element,
  $pos 
)

Definition at line 128 of file xml_utils.php.

129 {
130  $ar1=array_slice($ary, 0, $pos);
131  $ar1[]=$element;
132  $ary=array_merge($ar1, array_slice($ary, $pos));
133 }
xml2ary ( $string)

Definition at line 29 of file xml_utils.php.

References _del_p().

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 }
_del_p(&$ary)
Definition: xml_utils.php:86