note 19035 added to function.xml-parse-into-struct
| From: | oknapp at soly dot de | Date: | Wed, 13 Feb 2002 18:27:31 +0000 |
| Subject: | note 19035 added to function.xml-parse-into-struct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-26572@lists.php.net to get a copy of this message | ||
I modified gdemartini@bol.com.br's code a bit to satisfy my needs. I needed a more object
orientated aspect (at least this is in my opinion) and so, a object has a tag, attributes, values
and children. The children itself are objects again. Is the information identified by the
"complete" type.
That's the code:
-----------------------------
class xmltree{
function GetChildren($vals, &$i){
$children = array();
$all = array();
$all[0] = array();
$all[1] = array();
if ($vals[$i]['value']){
array_push($children, $vals[$i]['value']);
}
while (++$i < count($vals)){
switch ($vals[$i]['type']){
case 'cdata':
array_push($children, $vals[$i]['value']);
break;
case 'complete': //values
array_push($all[0], array('tag' => $vals[$i]['tag'],
'attributes' => $vals[$i]['attributes'], 'value' =>
$vals[$i]['value']));
break;
case 'open': //objects
$tmp = $this->GetChildren($vals, $i);
array_push($all[1], array('tag' => $vals[$i]['tag'],
'attributes' => $vals[$i]['attributes'], 'values' => $tmp[0],
'children' => $tmp[1]));
break;
case 'close':
return $all;
}
}
}
function GetXMLTree($vals, $index){
$tree = array();
$i = 0;
$tmp = $this->GetChildren($vals, $i);
array_push($tree,array('tag'=>$vals[$i]['tag'],
'attributes'=>$vals[$i]['attributes'], 'values' => $tmp[0],
'children' => $tmp[1]));
return $tree;
}
}
-------------------------------------
Hope that this may be of help for somebody. If you know how to implement this piece of code a bit
smoother, especially the $tmp array I would be very pleased, if you let me know how....
--
http://www.php.net/manual/en/function.xml-parse-into-struct.php
http://master.php.net/manage/user-notes.php?action=edit+19035
http://master.php.net/manage/user-notes.php?action=delete+19035
http://master.php.net/manage/user-notes.php?action=reject+19035