note 45052 added to function.xml-parse-into-struct

From: Date: Tue, 24 Aug 2004 15:51:39 +0000
Subject: note 45052 added to function.xml-parse-into-struct
Groups: php.notes 
Request: Send a blank email to php-notes+get-75206@lists.php.net to get a copy of this message
I would just add something about the solution proposed by "gordon at kanazawa dot ac dot jp". Something went wrong about the fact that "cdata" are treated the same way as "complete" in the xml_get_children first and in the xml_get_child then. When ? with this method, something like this : <tagA> <tagB>valueB</tagB> valueA <tagC>valueC</tagC> </tagA> is considered as the same thing as <tagA> <tagB>valueB</tagB> <tagA>valueA</tagA> <tagC>valueC</tagC> </tagA> The algorithm is nomore bijective. Even if it might not be perfect :-) i propose a little modification of the xml_get_child function : function xml_get_child(&$v, $children=NULL) { $c = array(); // the "child" if (isset($v['tag']) && $v['type'] != 'cdata') $c['tag'] = $v['tag']; if (isset($v['value'])) $c['value'] = $v['value']; if (isset($v['attributes'])) $c['attributes'] = $v['attributes']; if (is_array($children)) $c['children'] = $children; return $c; } moreover, the following function will take the tree (made from the xml_get_tree function) as a parameter and build the xml content back /* // setTree // set xml Tree from a well-formed array // manage indentation // Parameters are : // $1 (needed) : Tree to transform // $2 (optional) : Indentation type (default is one tabulation) // $3 (optional) : First indent (default is empty string) */ function setTree($Atree, $indentType = ' ', $Sindent = '') { if (! isset ($Atree['tag']) && isset ($Atree['value'])) return ($Sindent.trim($Atree['value'])."\r\n") ; else { $Stemp = $Sindent.'<'.$Atree['tag'].' ' ; if (isset ($Atree['attributes']) && is_array ($Atree['attributes'])) { foreach ($Atree['attributes'] as $Aattribute => $Svalue) $Stemp .= $Aattribute.'="'.$Svalue.'" ' ; } if (! isset ($Atree['children']) && ! isset ($Atree['value'])) $Stemp .= '/>'."\r\n" ; else { $Stemp .= '>'."\r\n" ; if (isset ($Atree['value'])) $Stemp .= $Sindent.$indentType.trim($Atree['value'])."\r\n" ; if (isset ($Atree['children']) && is_array ($Atree['children'])) foreach ($Atree['children'] as $Achild) $Stemp .= $this->setTree($Achild, $indentType, $Sindent.$indentType) ; $Stemp .= $Sindent.'</'.$Atree['tag'].'>'."\r\n" ; } return ($Stemp) ; } } ---- Manual Page -- http://www.php.net/manual/en/function.xml-parse-into-struct.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+45052 Delete -- http://master.php.net/manage/user-notes.php?action=delete+45052&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+45052&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#75206) next »