4 * xmlize.php - xmlize() is by Hans Anderson, {@link http://www.hansanderson.com/contact/}
6 * Ye Ole "Feel Free To Use it However" License [PHP, BSD, GPL].
7 * some code in xml_depth is based on code written by other PHPers
8 * as well as one Perl script. Poor programming practice and organization
9 * on my part is to blame for the credit these people aren't receiving.
10 * None of the code was copyrighted, though.
12 * @author Hans Anderson
13 * @version This is a stable release, 1.0. I don't foresee any changes, but you
14 * might check {@link http://www.hansanderson.com/php/xml/} to see
19 * Create xml formatted output from an array.
23 * $xml = xmlize($array);
25 * See the function {@link traverse_xmlize()} for information about the
26 * structure of the array, it's much easier to explain by showing you.
27 * Be aware that the array is somewhat tricky. I use xmlize all the time,
28 * but still need to use {@link traverse_xmlize()} quite often to show me the structure!
30 * THIS IS A PHP 5 VERSION:
32 * This modified version basically has a new optional parameter
33 * to specify an OUTPUT encoding. If not specified, it defaults to UTF-8.
34 * I recommend you to read this PHP bug. There you can see how PHP4, PHP5.0.0
35 * and PHP5.0.2 will handle this.
36 * {@link http://bugs.php.net/bug.php?id=29711}
40 * @author Hans Anderson
41 * @param array $data The array to be converted
42 * @param int $WHITE If set to 1 allows the parser to skip "space" characters in xml document. Default is 0
43 * @param string $encoding Specify an OUTPUT encoding. If not specified, it defaults to UTF-8.
46 function xmlize($data, $WHITE=1, $encoding='UTF-8') {
49 $vals = $index = $array = array();
50 $parser = xml_parser_create($encoding);
51 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING
, 0);
52 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE
, $WHITE);
53 xml_parse_into_struct($parser, $data, $vals, $index);
54 xml_parser_free($parser);
59 // XML file is invalid or empty, return false
63 $tagname = $vals[$i]['tag'];
64 if ( isset ($vals[$i]['attributes'] ) )
66 $array[$tagname]['@'] = $vals[$i]['attributes'];
68 $array[$tagname]['@'] = array();
71 $array[$tagname]["#"] = xml_depth($vals, $i);
78 * @internal You don't need to do anything with this function, it's called by
79 * xmlize. It's a recursive function, calling itself as it goes deeper
80 * into the xml levels. If you make any improvements, please let me know.
83 function xml_depth($vals, &$i) {
86 if ( isset($vals[$i]['value']) )
88 array_push($children, $vals[$i]['value']);
91 while (++
$i < count($vals)) {
93 switch ($vals[$i]['type']) {
97 if ( isset ( $vals[$i]['tag'] ) )
99 $tagname = $vals[$i]['tag'];
104 if ( isset ( $children[$tagname] ) )
106 $size = sizeof($children[$tagname]);
111 if ( isset ( $vals[$i]['attributes'] ) ) {
112 $children[$tagname][$size]['@'] = $vals[$i]["attributes"];
116 $children[$tagname][$size]['#'] = xml_depth($vals, $i);
122 array_push($children, $vals[$i]['value']);
126 $tagname = $vals[$i]['tag'];
128 if( isset ($children[$tagname]) )
130 $size = sizeof($children[$tagname]);
135 if( isset ( $vals[$i]['value'] ) )
137 $children[$tagname][$size]["#"] = $vals[$i]['value'];
139 $children[$tagname][$size]["#"] = '';
142 if ( isset ($vals[$i]['attributes']) ) {
143 $children[$tagname][$size]['@']
144 = $vals[$i]['attributes'];
163 * This helps you understand the structure of the array {@link xmlize()} outputs
165 * Function by acebone@f2s.com, a HUGE help!<br>
168 * traverse_xmlize($xml, 'xml_');
169 * print '<pre>' . implode("", $traverse_array . '</pre>';
171 * @author acebone@f2s.com
172 * @param array $array ?
173 * @param string $arrName ?
174 * @param int $level ?
176 * @todo Finish documenting this function
178 function traverse_xmlize($array, $arrName = 'array', $level = 0) {
180 foreach($array as $key=>$val)
182 if ( is_array($val) )
184 traverse_xmlize($val, $arrName . '[' . $key . ']', $level +
1);
186 $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";