note 69285 added to function.xml-set-element-handler
| From: | rubentrancoso at gmail dot com | Date: | Tue, 29 Aug 2006 20:44:35 +0000 |
| Subject: | note 69285 added to function.xml-set-element-handler | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-116488@lists.php.net to get a copy of this message | ||
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>";
----
Server IP: 66.163.161.117
Probable Submitter: 201.47.15.51
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.xml-set-element-handler.php
Edit -- https://master.php.net/note/edit/69285
Del: integrated -- https://master.php.net/note/delete/69285/integrated
Del: useless -- https://master.php.net/note/delete/69285/useless
Del: bad code -- https://master.php.net/note/delete/69285/bad+code
Del: spam -- https://master.php.net/note/delete/69285/spam
Del: non-english -- https://master.php.net/note/delete/69285/non-english
Del: in docs -- https://master.php.net/note/delete/69285/in+docs
Del: other reasons-- https://master.php.net/note/delete/69285
Reject -- https://master.php.net/note/reject/69285
Search -- https://master.php.net/manage/user-notes.php