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);
58 $tagname = $vals[$i]['tag'];
59 if ( isset ($vals[$i]['attributes'] ) )
61 $array[$tagname]['@'] = $vals[$i]['attributes'];
63 $array[$tagname]['@'] = array();
66 $array[$tagname]["#"] = xml_depth($vals, $i);
73 * @internal You don't need to do anything with this function, it's called by
74 * xmlize. It's a recursive function, calling itself as it goes deeper
75 * into the xml levels. If you make any improvements, please let me know.
78 function xml_depth($vals, &$i) {
81 if ( isset($vals[$i]['value']) )
83 array_push($children, $vals[$i]['value']);
86 while (++
$i < count($vals)) {
88 switch ($vals[$i]['type']) {
92 if ( isset ( $vals[$i]['tag'] ) )
94 $tagname = $vals[$i]['tag'];
99 if ( isset ( $children[$tagname] ) )
101 $size = sizeof($children[$tagname]);
106 if ( isset ( $vals[$i]['attributes'] ) ) {
107 $children[$tagname][$size]['@'] = $vals[$i]["attributes"];
111 $children[$tagname][$size]['#'] = xml_depth($vals, $i);
117 array_push($children, $vals[$i]['value']);
121 $tagname = $vals[$i]['tag'];
123 if( isset ($children[$tagname]) )
125 $size = sizeof($children[$tagname]);
130 if( isset ( $vals[$i]['value'] ) )
132 $children[$tagname][$size]["#"] = $vals[$i]['value'];
134 $children[$tagname][$size]["#"] = '';
137 if ( isset ($vals[$i]['attributes']) ) {
138 $children[$tagname][$size]['@']
139 = $vals[$i]['attributes'];
158 * This helps you understand the structure of the array {@link xmlize()} outputs
160 * Function by acebone@f2s.com, a HUGE help!<br>
163 * traverse_xmlize($xml, 'xml_');
164 * print '<pre>' . implode("", $traverse_array . '</pre>';
166 * @author acebone@f2s.com
167 * @param array $array ?
168 * @param string $arrName ?
169 * @param int $level ?
171 * @todo Finish documenting this function
173 function traverse_xmlize($array, $arrName = 'array', $level = 0) {
175 foreach($array as $key=>$val)
177 if ( is_array($val) )
179 traverse_xmlize($val, $arrName . '[' . $key . ']', $level +
1);
181 $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";