note 10069 deleted from function.xml-parse-into-struct by sniper

From: Date: Fri, 05 Dec 2003 00:32:18 +0000
Subject: note 10069 deleted from function.xml-parse-into-struct by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-61421@lists.php.net to get a copy of this message
Note Submitter: gdemartini@bol.com.br ---- I wrote a function to put an entire XML in a tree form. Each node has the following structure: /-> tag node +--> attributes \-> children So we can traverse de tree more easly in a recursive way. function GetChildren($vals, &$i) { $children = array(); if ($vals[$i]['value']) array_push($children, $vals[$i]['value']); while (++$i < count($vals)) // so pra nao botar while true ;-) { switch ($vals[$i]['type']) { case 'cdata': array_push($children, $vals[$i]['value']); break; case 'complete': array_push($children, array('tag' => $vals[$i]['tag'], 'attributes' => $vals[$i]['attributes'])); break; case 'open': array_push($children, array('tag' => $vals[$i]['tag'], 'attributes' => $vals[$i]['attributes'], 'children' => GetChildren($vals, $i))); break; case 'close': return $children; } } } function GetXMLTree($file) { $data = implode('', file($file)); $p = xml_parser_create(); xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($p, $data, &$vals, &$index); xml_parser_free($p); $tree = array(); $i = 0; array_push($tree, array('tag' => $vals[$i]['tag'], 'attributes' => $vals[$i]['attributes'], 'children' => GetChildren($vals, $i))); return $tree; } The XML and the tree generated are the following: <form> <section name="Data"> <editfield label="Text:" name="poll_text" length="255" size="56" required="T"/> <memofield label="Options" name="options" cols="56" rows="5" required="T"/> </section> </form> Tree array Array ( [0] => Array ( [tag] => FORM [attributes] => [children] => Array ( [0] => Array ( [tag] => SECTION [attributes] => Array ( [NAME] => Data ) [children] => Array ( [0] => Array ( [tag] => EDITFIELD [attributes] => Array ( [LABEL] => Text: [NAME] => poll_text [LENGTH] => 255 [SIZE] => 56 [REQUIRED] => T ) ) [1] => Array ( [tag] => MEMOFIELD [attributes] => Array ( [LABEL] => Options [NAME] => options [COLS] => 56 [ROWS] => 5 [REQUIRED] => T ) ) ) ) ) ) )

« previous php.notes (#61421) next »