note 44764 deleted from function.xml-parse-into-struct by tomsommer
| From: | tomsommer@php.net | Date: | Mon, 16 Aug 2004 03:08:15 +0000 |
| Subject: | note 44764 deleted from function.xml-parse-into-struct by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-74747@lists.php.net to get a copy of this message | ||
Note Submitter: jaskolka at bellsouth dot net
----
Not sure if this has already been done, but this does the exact reverse of xml_parse_into_struct.
Feel free to reply and/or email me back on suggestions, improvements, etc. :)
<?
// Written by Jason Jaskolka
// Date 8/15/04
// Documentation
// string struct2XML(array xml, string strFileOutput, bool blnPreserveSpace = true)
// Writes XML based on value array of xml_parse_into_struct and returns xml string as well as
writes to file if strFileOutput provided
// Dependencies: PHP 4.3 or greater
function struct2XML ($xml, $strFileOutput = "", $blnPreserveSpace = true)
{
foreach($xml as $key=>$element)
{
if (($element['type'] == "complete") or ($element['type'] ==
"open"))
{
if (empty($element['attributes']) == false)
{
$strAttributes = "";
foreach ($element['attributes'] as $strAttribute=>$strValue)
{
$strAttributes .= " " . $strAttribute . "=\"" . $strValue .
"\"";
}
}
if ($element['type'] == "complete")
{
$strCloseTag = "</" . $element['tag'] . ">";
}
$strResults .= "<" . $element['tag'] . $strAttributes . ">"
. $element['value'] . $strCloseTag;
}
elseif ($element['type'] == "cdata")
{
if ($blnPreserveSpace == true)
{
$strResults .= $element['value'];
}
else
{
$strResults .= trim($element['value']);
}
}
elseif ($element['type'] == "close")
{
$strResults .= "</" . $element['tag'] . ">";
}
}
if (empty($strFileOutput) == false)
{
file_put_contents($strFileOutput, $strResults);
}
return $strResults;
}
?>