note 53473 added to ref.xml

From: Date: Thu, 02 Jun 2005 14:41:06 +0000
Subject: note 53473 added to ref.xml
Groups: php.notes 
Request: Send a blank email to php-notes+get-89801@lists.php.net to get a copy of this message
I just wanted to post a correction to the script proposed by mv, a pair of posts below. That script is quite good and really simple and fast, but i think there is a misunderstanding of the type "complete" in the array that the function xml_parse_into_struct creates. When the parser meets an open tag FOLLOWED by another open tag, it classifies the first tag as "open" in the structure: <tagone> <-------- "open" tag <tagtwo> When it finds a close tag FOLLOWING another close tag, it classifies the first tag as "close" : </tagtwo> </tagone> <--------- "close" tag Here we are : when the parser finds an open tag, followed by content, followed by a close tag, it classifies the WHOLE thing as "complete": <tag>hi my name is</tag> <------- "complete" tag mv's script php2xml echoed "complete" tags as : <tag attribute="value" /> , which (my opinion!) is wrong. The rest is classified as "cdata", and can be trashed. Now, i report a slight modification of mv's script; if my interpretation is wrong, let me know! <? $file = "menu_items.xml"; /** * Get contents from XML file and insert into an array * * @param string $file * @return mixed */ function xml2php($file) { $xml_parser = xml_parser_create(); if (!($fp = fopen($file, "r"))) { die("unable to open XML"); } $contents = fread($fp, filesize($file)); fclose($fp); xml_parse_into_struct($xml_parser, $contents, $arr_vals); xml_parser_free($xml_parser); return $arr_vals; } /** * Convert php array to XML * * @param mixed $array_haystack * @return string */ function php2xml($array_haystack) { $xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n"; if ((!empty($array_haystack)) AND (is_array($array_haystack))) { foreach ($array_haystack as $xml_key => $xml_value) { switch ($xml_value["type"]) { case "open": $xml .= str_repeat("\t", $xml_value["level"] - 1); $xml .= "<" . strtolower($xml_value["tag"]); if(isset($xml_value["attributes"])) $xml .= parseattributes($xml_value["attributes"]); $xml .= ">\n"; break; case "complete": $xml .= str_repeat("\t", $xml_value["level"] - 1); $xml .= "<" . strtolower($xml_value["tag"]); if(isset($xml_value["attributes"])) $xml .= parseattributes($xml_value["attributes"]); $xml .= ">" . $xml_value["value"]; $xml .= "</" . strtolower($xml_value["tag"]); $xml .= ">\n"; break; case "close": $xml .= str_repeat("\t", $xml_value["level"] - 1); $xml .= "</" . strtolower($xml_value["tag"]); $xml .= ">\n"; break; default: break; } } } return $xml; } /** * Convert php attributes array to XML * * @param mixed $attributes * @return string */ function parseattributes($attributes) { $xml = ""; foreach ($attributes as $attribute_key => $attribute_value) { $xml .= sprintf(' %s="%s"', strtolower($attribute_key), $attribute_value); } return $xml; } /** * Output content as XML * * @param string $content */ function output_xml($content) { header("Content-Type: application/xml; charset=ISO-8859-1"); header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: ". gmdate("D, d M Y H:i:s") ." GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); print $content; } $arr_xml = xml2php($file); $xml = php2xml($arr_xml); output_xml($xml); ?> Bye, lorenzo ---- Manual Page -- http://www.php.net/manual/en/ref.xml.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+53473 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53473&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53473&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53473&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53473&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53473&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+53473&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#89801) next »