All pastes #244767 Raw Edit

Miscellany

public text v1 · immutable
#244767 ·published 2006-11-10 04:08 UTC
rendered paste body
/**
 * xml_from_array
 * This takes a one dimensional array and
 * creates a XML document form it for use
 * primarly by the ajax mojo
 */
function xml_from_array($array,$callback=0,$type='') {
        if (empty($type)){
        foreach ($array as $key=>$value) {
                if (is_numeric($key)) { $key = 'item'; }
                if (is_array($value)) {
                        $value = xml_from_array($value,1);
                        $string .= "\t<content div=\"$key\">$value</content>\n";
                }
                else {
                        /* We need to escape the value */
                        $string .= "\t<content div=\"$key\"><![CDATA[$value]]></content>\n";
                }
        } // end foreach elements
        } elseif ($type == "itunesdb"){
        foreach ($array as $key=>$value) {
                if (is_array($value)) {
                        $value = xml_from_array($value,1,$type);
                        $string .= "\t<$key>\n$value\t</$key>\n";
                }
                else {
                        if (is_numeric($value)) {
                        $string .= "\t\t<$key>$value</$key>\n";
                        } else {
                        /* We need to escape the value */
                        $string .= "\t\t<$key><![CDATA[$value]]></$key>\n";
                        }
                }

        }
        }
        if (!$callback) {
                $string = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<root>\n" . $string . "</root>\n";
        }

        return $string;

} // xml_from_array