note 69285 deleted from function.xml-set-element-handler by betz

From: Date: Tue, 29 Aug 2006 21:34:58 +0000
Subject: note 69285 deleted from function.xml-set-element-handler by betz
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-116491@lists.php.net to get a copy of this message
Note Submitter: rubentrancoso at gmail dot com ---- My 25 cents. This example show how to parse a XML in a associative array tree. <?xml version="1.0" encoding="ISO-8859-1"?> <flows start="menu principal"> <flow id="0" name="menu principal"> <step id="0" name="escolher atividade"> <case name="encontrou" value="1"/> <case name="não encontrou" value="0"/> </step> </flow> <flow id="1" name="cadastro de aluno"> <step id="0" name="procura aluno"> <case name="encontrou" value="1"/> <case name="não encontrou" value="0"/> </step> </flow> </flows> <?php $file = "flow/flow.xml"; $depth = 0; $tree = array(); $tree['name'] = "root"; $stack[count($stack)] = &$tree; function startElement($parser, $name, $attrs) { global $depth; global $stack; global $tree; $element = array(); $element['name'] = $name; foreach ($attrs as $key => $value) { //echo $key."=".$value; $element[$key]=$value; } $last = &$stack[count($stack)-1]; $last[count($last)-1] = &$element; $stack[count($stack)] = &$element; $depth++; } function endElement($parser, $name) { global $depth; global $stack; array_pop($stack); $depth--; } $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); if (!($fp = fopen($file, "r"))) { die("could not open XML input"); } while ($data = fread($fp, 4096)) { if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } } xml_parser_free($xml_parser); $tree = $stack[0][0]; echo "<pre>"; print_r($tree); echo "</pre>";

« previous php.notes (#116491) next »