3 /* xmlize() is by Hans Anderson, me@hansanderson.com
5 * Ye Ole "Feel Free To Use it However" License [PHP, BSD, GPL].
6 * some code in xml_depth is based on code written by other PHPers
7 * as well as one Perl script. Poor programming practice and organization
8 * on my part is to blame for the credit these people aren't receiving.
9 * None of the code was copyrighted, though.
11 * This is a stable release, 1.0. I don't foresee any changes, but you
12 * might check http://www.hansanderson.com/php/xml/ to see
14 * usage: $xml = xmlize($array);
16 * See the function traverse_xmlize() for information about the
17 * structure of the array, it's much easier to explain by showing you.
18 * Be aware that the array is somewhat tricky. I use xmlize all the time,
19 * but still need to use traverse_xmlize quite often to show me the structure!
23 function xmlize($data, $WHITE=1) {
26 $vals = $index = $array = array();
27 $parser = xml_parser_create();
28 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING
, 0);
29 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE
, $WHITE);
30 xml_parse_into_struct($parser, $data, $vals, $index);
31 xml_parser_free($parser);
35 $tagname = $vals[$i]['tag'];
36 if ( isset ($vals[$i]['attributes'] ) )
38 $array[$tagname]['@'] = $vals[$i]['attributes'];
40 $array[$tagname]['@'] = array();
43 $array[$tagname]["#"] = xml_depth($vals, $i);
51 * You don't need to do anything with this function, it's called by
52 * xmlize. It's a recursive function, calling itself as it goes deeper
53 * into the xml levels. If you make any improvements, please let me know.
58 function xml_depth($vals, &$i) {
61 if ( isset($vals[$i]['value']) )
63 array_push($children, $vals[$i]['value']);
66 while (++
$i < count($vals)) {
68 switch ($vals[$i]['type']) {
72 if ( isset ( $vals[$i]['tag'] ) )
74 $tagname = $vals[$i]['tag'];
79 if ( isset ( $children[$tagname] ) )
81 $size = sizeof($children[$tagname]);
86 if ( isset ( $vals[$i]['attributes'] ) ) {
87 $children[$tagname][$size]['@'] = $vals[$i]["attributes"];
91 $children[$tagname][$size]['#'] = xml_depth($vals, $i);
97 array_push($children, $vals[$i]['value']);
101 $tagname = $vals[$i]['tag'];
103 if( isset ($children[$tagname]) )
105 $size = sizeof($children[$tagname]);
110 if( isset ( $vals[$i]['value'] ) )
112 $children[$tagname][$size]["#"] = $vals[$i]['value'];
114 $children[$tagname][$size]["#"] = '';
117 if ( isset ($vals[$i]['attributes']) ) {
118 $children[$tagname][$size]['@']
119 = $vals[$i]['attributes'];
137 /* function by acebone@f2s.com, a HUGE help!
139 * this helps you understand the structure of the array xmlize() outputs
142 * traverse_xmlize($xml, 'xml_');
143 * print '<pre>' . implode("", $traverse_array . '</pre>';
148 function traverse_xmlize($array, $arrName = "array", $level = 0) {
150 foreach($array as $key=>$val)
152 if ( is_array($val) )
154 traverse_xmlize($val, $arrName . "[" . $key . "]", $level +
1);
156 $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";