Debian Codegraph @

contrib/kannel-monitor/xmltoarray.php

raw
1 <?php
2 /**
3 * XMLToArray Generator Class
4 * @author : MA Razzaque Rupom <[email protected]>, <[email protected]>
5 * Moderator, phpResource (LINK1http://groups.yahoo.com/group/phpresource/LINK1)
6 * URL: LINK2http://www.rupom.infoLINK2
7 * @version : 1.0
8 * @date 06/05/2006
9 * Purpose : Creating Hierarchical Array from XML Data
10 * Released : Under GPL
11 */
12
13 class XmlToArray
14 {
15
16 var $xml='';
17
18 /**
19 * Default Constructor
20 * @param $xml = xml data
21 * @return none
22 */
23
24 function XmlToArray($xml)
25 {
26 $this->xml = $xml;
27 }
28
29 /**
30 * _struct_to_array($values, &$i)
31 *
32 * This is adds the contents of the return xml into the array for easier processing.
33 * Recursive, Static
34 *
35 * @access private
36 * @param array $values this is the xml data in an array
37 * @param int $i this is the current location in the array
38 * @return Array
39 */
40
41 function _struct_to_array($values, &$i)
42 {
43 $child = array();
44 if (isset($values[$i]['value'])) array_push($child, $values[$i]['value']);
45
46 while ($i++ < count($values)) {
47 switch ($values[$i]['type']) {
48 case 'cdata':
49 array_push($child, $values[$i]['value']);
50 break;
51
52 case 'complete':
53 $name = $values[$i]['tag'];
54 if(!empty($name)){
55 $child[$name]= ($values[$i]['value'])?($values[$i]['value']):'';
56 if(isset($values[$i]['attributes'])) {
57 $child[$name] = $values[$i]['attributes'];
58 }
59 }
60 break;
61
62 case 'open':
63 $name = $values[$i]['tag'];
64 $size = isset($child[$name]) ? sizeof($child[$name]) : 0;
65 $child[$name][$size] = $this->_struct_to_array($values, $i);
66 break;
67
68 case 'close':
69 return $child;
70 break;
71 }
72 }
73 return $child;
74 }//_struct_to_array
75
76 /**
77 * createArray($data)
78 *
79 * This is adds the contents of the return xml into the array for easier processing.
80 *
81 * @access public
82 * @param string $data this is the string of the xml data
83 * @return Array
84 */
85 function createArray()
86 {
87 $xml = $this->xml;
88 $values = array();
89 $index = array();
90 $array = array();
91 $parser = xml_parser_create();
92 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
93 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
94 xml_parse_into_struct($parser, $xml, $values, $index);
95 xml_parser_free($parser);
96 $i = 0;
97 $name = $values[$i]['tag'];
98 $array[$name] = isset($values[$i]['attributes']) ? $values[$i]['attributes'] : '';
99 $array[$name] = $this->_struct_to_array($values, $i);
100 return $array;
101 }//createArray
102
103
104 }//XmlToArray
105 ?>