note 17990 added to function.xml-parse-into-struct
| From: | voncken at mailandnews dot com | Date: | Sun, 06 Jan 2002 10:24:21 +0000 |
| Subject: | note 17990 added to function.xml-parse-into-struct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-24460@lists.php.net to get a copy of this message | ||
I needed something a bit different:
this is the result of my xml2array function (from the moldb example)
Array
(
[molecule] => Array
(
[0] => Array
(
[name] => Alanine
[symbol] => ala
[code] => A
[type] => hydrophobic
)
[1] => Array
(
[name] => Lysine
[symbol] => lys
[code] => K
[type] => charged
)
)
)
function xml2array($data)
{
//mvo voncken@mailandnews.com
//original ripped from on the php-manual:gdemartini@bol.com.br
//to be used for data retrieval(result-structure is Data oriented)
$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' => ));
$tree = GetChildren($vals, $i);
return $tree;
}
function GetChildren($vals, &$i)
{
$children = array();
if ($vals[$i]['value']) array_push($children, $vals[$i]['value']);
$prevtag = "";
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':
$children[ strtolower($vals[$i]['tag']) ] = $vals[$i]['value'];
break;
case 'open':
//restartindex on unique tag-name
$j++;
if ($prevtag <> $vals[$i]['tag']) {
$j = 0;
$prevtag = $vals[$i]['tag'];
}
$children[ strtolower($vals[$i]['tag']) ][$j] = GetChildren($vals,$i);
break;
case 'close':
return $children;
}
}
}
--
http://www.php.net/manual/en/function.xml-parse-into-struct.php
http://master.php.net/manage/user-notes.php?action=edit+17990
http://master.php.net/manage/user-notes.php?action=delete+17990
http://master.php.net/manage/user-notes.php?action=reject+17990