3 /* xmlize() is by Hans Anderson, me@hansanderson.com
\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($array);
\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 somewhat tricky. I use xmlize all the time,
\r
19 * but still need to use traverse_xmlize quite often to show me the structure!
\r
21 ## THIS IS A PHP 5 VERSION:
\r
23 > attached is the modified script. Basically it has a new optional parameter
\r
24 > to specify an OUTPUT encoding. If not specified, it defaults to UTF-8.
\r
25 > I recommend you to read this PHP bug. There you can see how PHP4, PHP5.0.0
\r
26 > and PHP5.0.2 will handle this.
\r
27 > http://bugs.php.net/bug.php?id=29711
\r
33 function xmlize($data, $WHITE=1, $encoding='UTF-8') {
\r
35 $data = trim($data);
\r
36 $vals = $index = $array = array();
\r
37 $parser = xml_parser_create($encoding);
\r
38 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
\r
39 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $WHITE);
\r
40 xml_parse_into_struct($parser, $data, $vals, $index);
\r
41 xml_parser_free($parser);
\r
45 $tagname = $vals[$i]['tag'];
\r
46 if ( isset ($vals[$i]['attributes'] ) )
\r
48 $array[$tagname]['@'] = $vals[$i]['attributes'];
\r
50 $array[$tagname]['@'] = array();
\r
53 $array[$tagname]["#"] = xml_depth($vals, $i);
\r
61 * You don't need to do anything with this function, it's called by
\r
62 * xmlize. It's a recursive function, calling itself as it goes deeper
\r
63 * into the xml levels. If you make any improvements, please let me know.
\r
68 function xml_depth($vals, &$i) {
\r
69 $children = array();
\r
71 if ( isset($vals[$i]['value']) )
\r
73 array_push($children, $vals[$i]['value']);
\r
76 while (++$i < count($vals)) {
\r
78 switch ($vals[$i]['type']) {
\r
82 if ( isset ( $vals[$i]['tag'] ) )
\r
84 $tagname = $vals[$i]['tag'];
\r
89 if ( isset ( $children[$tagname] ) )
\r
91 $size = sizeof($children[$tagname]);
\r
96 if ( isset ( $vals[$i]['attributes'] ) ) {
\r
97 $children[$tagname][$size]['@'] = $vals[$i]["attributes"];
\r
101 $children[$tagname][$size]['#'] = xml_depth($vals, $i);
\r
107 array_push($children, $vals[$i]['value']);
\r
111 $tagname = $vals[$i]['tag'];
\r
113 if( isset ($children[$tagname]) )
\r
115 $size = sizeof($children[$tagname]);
\r
120 if( isset ( $vals[$i]['value'] ) )
\r
122 $children[$tagname][$size]["#"] = $vals[$i]['value'];
\r
124 $children[$tagname][$size]["#"] = '';
\r
127 if ( isset ($vals[$i]['attributes']) ) {
\r
128 $children[$tagname][$size]['@']
\r
129 = $vals[$i]['attributes'];
\r
147 /* function by acebone@f2s.com, a HUGE help!
\r
149 * this helps you understand the structure of the array xmlize() outputs
\r
152 * traverse_xmlize($xml, 'xml_');
\r
153 * print '<pre>' . implode("", $traverse_array . '</pre>';
\r
158 function traverse_xmlize($array, $arrName = "array", $level = 0) {
\r
160 foreach($array as $key=>$val)
\r
162 if ( is_array($val) )
\r
164 traverse_xmlize($val, $arrName . "[" . $key . "]", $level + 1);
\r
166 $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";
\r