Theia installation has been updated - September 10, 2011
« May 2012
S M T W T F S
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    
Theia
Serialize array / object as XML in PHP « home
icon
posted on 12:31 - 06 December 2010 | posted by Lev
last modified on 12:32 - 06 December 2010 | last modified by Lev
As far as I know, there doesn't seem to be a built in way to do this with PHP's simplexml class, so I wrote a simple function you're free to use:
 
PHP CODE
function SerializeAsXML ($obj$depth 0)
    {
    if (
is_array($obj) || is_object($obj))
        {
        for (
$i 0$i <= $depth$i++)
            {
            
$tabs .= "\t";
            }
        foreach (
$obj as $o_i => $o_v)
            {
            
$useTab false;
            
$d .= $tabs "<{$o_i}>";
            if (
is_array($o_v) || is_object($o_v))
                {
                
$tabs++;
                
$d .= "\n" SerializeAsXML($o_v, ($depth 1));
                
$useTab true;
                }
            else
                {
                
$d .= $o_v;
                }
            if (
$useTab)
                {
                
$d .= $tabs;
                }
            
$d .= "</{$o_i}>\n";
            }
        }
    return 
$d;
    } 
 
This accepts either an array or an object and takes care of the tabbing/formatting (at least in the manner I like; you may tweak it to your liking).
tags
post reply
Bookmark item @
bookmarkbookmarkbookmarkbookmarkbookmark