3 /* xmlize() is by Hans Anderson, www.hansanderson.com/contact/
\r
5 * Ye Ole "Feel Free To Use it However" License [PHP, BSD, GPL].
\r
6 * some code in xml_depth is based on code written by other PHPers
\r
7 * as well as one Perl script. Poor programming practice and organization
\r
8 * on my part is to blame for the credit these people aren't receiving.
\r
9 * None of the code was copyrighted, though.
\r
11 * This is a stable release, 1.0. I don't foresee any changes, but you
\r
12 * might check http://www.hansanderson.com/php/xml/ to see
\r
14 * usage: $xml = xmlize($xml_data);
\r
16 * See the function traverse_xmlize() for information about the
\r
17 * structure of the array, it's much easier to explain by showing you.
\r
18 * Be aware that the array is very complex. I use xmlize all the time,
\r
19 * but still need to use traverse_xmlize or print_r() quite often to
\r
20 * show me the structure!
\r
24 function xmlize($data, $WHITE=1) {
\r
26 $data = trim($data);
\r
27 $vals = $index = $array = array();
\r
28 $parser = xml_parser_create();
\r
29 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
\r
30 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $WHITE);
\r
31 if ( !xml_parse_into_struct($parser, $data, $vals, $index) )
\r
33 die(sprintf("XML error: %s at line %d",
\r
34 xml_error_string(xml_get_error_code($parser)),
\r
35 xml_get_current_line_number($parser)));
\r
38 xml_parser_free($parser);
\r
42 $tagname = $vals[$i]['tag'];
\r
43 if ( isset ($vals[$i]['attributes'] ) )
\r
45 $array[$tagname]['@'] = $vals[$i]['attributes'];
\r
47 $array[$tagname]['@'] = array();
\r
50 $array[$tagname]["#"] = xml_depth($vals, $i);
\r
57 * You don't need to do anything with this function, it's called by
\r
58 * xmlize. It's a recursive function, calling itself as it goes deeper
\r
59 * into the xml levels. If you make any improvements, please let me know.
\r
64 function xml_depth($vals, &$i) {
\r
65 $children = array();
\r
67 if ( isset($vals[$i]['value']) )
\r
69 array_push($children, $vals[$i]['value']);
\r
72 while (++$i < count($vals)) {
\r
74 switch ($vals[$i]['type']) {
\r
78 if ( isset ( $vals[$i]['tag'] ) )
\r
80 $tagname = $vals[$i]['tag'];
\r
85 if ( isset ( $children[$tagname] ) )
\r
87 $size = sizeof($children[$tagname]);
\r
92 if ( isset ( $vals[$i]['attributes'] ) ) {
\r
93 $children[$tagname][$size]['@'] = $vals[$i]["attributes"];
\r
96 $children[$tagname][$size]['#'] = xml_depth($vals, $i);
\r
102 array_push($children, $vals[$i]['value']);
\r
106 $tagname = $vals[$i]['tag'];
\r
108 if( isset ($children[$tagname]) )
\r
110 $size = sizeof($children[$tagname]);
\r
115 if( isset ( $vals[$i]['value'] ) )
\r
117 $children[$tagname][$size]["#"] = $vals[$i]['value'];
\r
119 $children[$tagname][$size]["#"] = '';
\r
122 if ( isset ($vals[$i]['attributes']) ) {
\r
123 $children[$tagname][$size]['@']
\r
124 = $vals[$i]['attributes'];
\r
141 /* function by acebone@f2s.com, a HUGE help!
\r
143 * this helps you understand the structure of the array xmlize() outputs
\r
146 * traverse_xmlize($xml, 'xml_');
\r
147 * print '<pre>' . implode("", $traverse_array . '</pre>';
\r
152 function traverse_xmlize($array, $arrName = "array", $level = 0) {
\r
154 foreach($array as $key=>$val)
\r
156 if ( is_array($val) )
\r
158 traverse_xmlize($val, $arrName . "[" . $key . "]", $level + 1);
\r
160 $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";
\r