note 44764 added to function.xml-parse-into-struct
| From: | jaskolka at bellsouth dot net | Date: | Mon, 16 Aug 2004 00:11:24 +0000 |
| Subject: | note 44764 added to function.xml-parse-into-struct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-74742@lists.php.net to get a copy of this message | ||
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;
}
?>
----
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+44764
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44764&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44764&report=yes
Search -- http://master.php.net/manage/user-notes.php