note 39363 added to function.xml-parse-into-struct

From: Date: Mon, 26 Jan 2004 01:36:05 +0000
Subject: note 39363 added to function.xml-parse-into-struct
Groups: php.notes 
Request: Send a blank email to php-notes+get-64184@lists.php.net to get a copy of this message
Many thanks to "dave at ovumdesign dot com" for his excellent routines based on the fine work of "gdemartini at bol dot com". I offer a further refinement below, which removes the duplication of the code to create a child node. The resulting tree is the same as in Dave's earlier version and so can be compacted using his "xml_condense_tree" function xml_get_child(&$v, $children=NULL) { $c = array(); // the "child" if (isset($v['tag'])) $c['tag'] = $v['tag']; if (isset($v['value'])) $c['value'] = $v['value']; if (isset($v['attributes'])) $c['attributes'] = $v['attributes']; if (is_array($children)) $c['children'] = $children; return $c; } function xml_get_children(&$values, &$i) { $children = array(); while ($i < count($values)) { $v = &$values[$i++]; switch ($v['type']) { case 'cdata': case 'complete': $children[] = xml_get_child($v); break; case 'open': $children[] = xml_get_child($v, xml_get_children($values, $i)); break; case 'close': break 2; // leave "while" loop } } return $children; } function xml_get_tree($file) { $data = join('', file($file)); $p = xml_parser_create(); xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($p, $data, $values); xml_parser_free($p); return xml_get_children($values, $i=0); } ---- Manual Page -- http://www.php.net/manual/en/function.xml-parse-into-struct.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39363 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39363&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39363&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#64184) next »