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.
13 * @author Hans Anderson
14 * @version This is a stable release, 1.0. I don't foresee any changes, but you
15 * might check {@link http://www.hansanderson.com/php/xml/} to see
16 * @copyright Hans Anderson
17 * @license Feel Free To Use it However
21 * Create xml formatted output from an array.
25 * $xml = xmlize($array);
27 * See the function {@link traverse_xmlize()} for information about the
28 * structure of the array, it's much easier to explain by showing you.
29 * Be aware that the array is somewhat tricky. I use xmlize all the time,
30 * but still need to use {@link traverse_xmlize()} quite often to show me the structure!
32 * THIS IS A PHP 5 VERSION:
34 * This modified version basically has a new optional parameter
35 * to specify an OUTPUT encoding. If not specified, it defaults to UTF-8.
36 * I recommend you to read this PHP bug. There you can see how PHP4, PHP5.0.0
37 * and PHP5.0.2 will handle this.
38 * {@link http://bugs.php.net/bug.php?id=29711}
42 * @author Hans Anderson
43 * @param array $data The array to be converted
44 * @param int $WHITE If set to 1 allows the parser to skip "space" characters in xml document. Default is 1
45 * @param string $encoding Specify an OUTPUT encoding. If not specified, it defaults to UTF-8.
48 function xmlize($data, $WHITE=1, $encoding='UTF-8') {
51 $vals = $index = $array = array();
52 $parser = xml_parser_create($encoding);
53 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING
, 0);
54 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE
, $WHITE);
55 xml_parse_into_struct($parser, $data, $vals, $index);
56 xml_parser_free($parser);
61 // XML file is invalid or empty, return false
65 $tagname = $vals[$i]['tag'];
66 if ( isset ($vals[$i]['attributes'] ) )
68 $array[$tagname]['@'] = $vals[$i]['attributes'];
70 $array[$tagname]['@'] = array();
73 $array[$tagname]["#"] = xml_depth($vals, $i);
80 * @internal You don't need to do anything with this function, it's called by
81 * xmlize. It's a recursive function, calling itself as it goes deeper
82 * into the xml levels. If you make any improvements, please let me know.
85 function xml_depth($vals, &$i) {
88 if ( isset($vals[$i]['value']) )
90 array_push($children, $vals[$i]['value']);
93 while (++
$i < count($vals)) {
95 switch ($vals[$i]['type']) {
99 if ( isset ( $vals[$i]['tag'] ) )
101 $tagname = $vals[$i]['tag'];
106 if ( isset ( $children[$tagname] ) )
108 $size = sizeof($children[$tagname]);
113 if ( isset ( $vals[$i]['attributes'] ) ) {
114 $children[$tagname][$size]['@'] = $vals[$i]["attributes"];
118 $children[$tagname][$size]['#'] = xml_depth($vals, $i);
124 array_push($children, $vals[$i]['value']);
128 $tagname = $vals[$i]['tag'];
130 if( isset ($children[$tagname]) )
132 $size = sizeof($children[$tagname]);
137 if( isset ( $vals[$i]['value'] ) )
139 $children[$tagname][$size]["#"] = $vals[$i]['value'];
141 $children[$tagname][$size]["#"] = '';
144 if ( isset ($vals[$i]['attributes']) ) {
145 $children[$tagname][$size]['@']
146 = $vals[$i]['attributes'];
165 * This helps you understand the structure of the array {@link xmlize()} outputs
167 * Function by acebone@f2s.com, a HUGE help!<br>
170 * traverse_xmlize($xml, 'xml_');
171 * print '<pre>' . implode("", $traverse_array . '</pre>';
173 * @author acebone@f2s.com
174 * @param array $array ?
175 * @param string $arrName ?
176 * @param int $level ?
178 * @todo Finish documenting this function
180 function traverse_xmlize($array, $arrName = 'array', $level = 0) {
182 foreach($array as $key=>$val)
184 if ( is_array($val) )
186 traverse_xmlize($val, $arrName . '[' . $key . ']', $level +
1);
188 $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";