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

From: Date: Sun, 24 Nov 2002 10:32:28 +0000
Subject: note 27130 added to function.xml-parse-into-struct
Groups: php.notes 
Request: Send a blank email to php-notes+get-39908@lists.php.net to get a copy of this message
wanna load object from xml file? this function take a tree returned by xml_get_tree() [dave@ovumdesign.com's version] and return an object. it can't work with attributes, i hate attributes (except id), but may be useful for you. - how to use : ------------------------- xml file ------------- <config> <images> <image id="1"> <name>my pet</name> <file>pet001.jpg</file> </image> <image id="2"> <name>harry's pet</name> <file>pet003.jpg</file> </image> </images> <library>lib_functions.php</library> </config> ---------------------------------------------- $config=xml_to_object($xml_tree); $config->library; #return "lib_functions.php" $config->images->image__1->name; #return "my pet" ---------------------------------------------- function xml_to_object($xml_tree,$root=false){ $object_name=$xml_tree['tag']; foreach ($xml_tree['children'] as $children){ $child_id=$children['attributes']['id']; $child_name=$children['tag']; $child_name.=($child_id) ? "__".$child_id:''; if (is_array($children['children'])) { $child_object=xml_to_object($children); $temp_object->$child_name=$child_object; } else { $temp_object->$child_name=$children['value']; } } if (!$root) $xml_object=$temp_object; else $xml_object->$object_name=$temp_object; return $xml_object; } also you can use this function for loading array from xml , but i suggest you to use kris@h3x.com's functions[ http://www.devdump.com/phpxml.php ]. function xml_to_array($xml_tree,$root=false){ $array_name=$xml_tree['tag']; foreach ($xml_tree['children'] as $children){ $child_id=$children['attributes']['id']; $child_name=$children['tag']; $child_name.=($child_id) ? "__".$child_id:''; if (is_array($children['children'])) { $child_array=xml_to_array($children); $temp_array[$child_name]=$child_array; } else { $temp_array[$child_name]=$children['value']; } } if (!$root) $xml_array=$temp_array; else $xml_array[$array_name]=$temp_array; return $xml_array; } -- http://www.php.net/manual/en/function.xml-parse-into-struct.php http://master.php.net/manage/user-notes.php?action=edit+27130 http://master.php.net/manage/user-notes.php?action=delete+27130 http://master.php.net/manage/user-notes.php?action=reject+27130

« previous php.notes (#39908) next »