note 52230 added to ref.xml
| From: | mvatbrazildotcom at osu1 dot php dot net | Date: | Mon, 25 Apr 2005 12:07:43 +0000 |
| Subject: | note 52230 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88524@lists.php.net to get a copy of this message | ||
here a exemple to parse XML file to an PHP array and PHP array to XML.
<?
$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"]);
$xml .= (!isset($xml_value["attributes"]))? ">\n": false;
break;
case "complete":
$xml .= str_repeat("\t", $xml_value["level"] - 1);
$xml .= "<" . strtolower($xml_value["tag"]);
$xml .= (!isset($xml_value["attributes"]))? ">\n": false;
break;
case "close":
$xml .= str_repeat("\t", $xml_value["level"] - 1);
$xml .= "</" . strtolower($xml_value["tag"]);
$xml .= (!isset($xml_value["attributes"]))? ">\n": false;
break;
default:
break;
}
if (isset($xml_value["attributes"])) {
foreach ($xml_value["attributes"] as $atribute_key => $atribute_value) {
$xml .= sprintf(' %s="%s"', strtolower($atribute_key), $atribute_value);
}
$xml .= ($xml_value["type"] == "complete")? " />\n":
">\n";
}
}
}
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);
?>
----
Manual Page -- http://www.php.net/manual/en/ref.xml.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52230
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52230&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52230&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52230&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52230&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52230&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52230&report=yes
Search -- http://master.php.net/manage/user-notes.php