/**
 * Converts attributes of an object to xml code
 *
 * Original obj2xml() function by <*****@*****.**>
 * as found on http://www.php.net/manual/en/function.get-defined-vars.php
 * Fixed and improved by Robin Johnson <*****@*****.**>
 *
 * @param   object  the source
 * @param   string  identication
 *
 * @access  public
 */
function obj2xml($v, $indent = '')
{
    $attr = '';
    foreach ($v as $key => $val) {
        if (is_string($key) && $key == '__attr') {
            continue;
        }
        // Check for __attr
        if (is_object($val->__attr)) {
            foreach ($val->__attr as $key2 => $val2) {
                $attr .= " {$key2}=\"{$val2}\"";
            }
        } else {
            $attr = '';
        }
        // Preserve data type information
        $attr .= ' type="' . gettype($val) . '"';
        if (is_array($val) || is_object($val)) {
            echo "{$indent}<{$key}{$attr}>\n";
            obj2xml($val, $indent . '  ');
            echo "{$indent}</{$key}>\n";
        } else {
            if (is_string($val) && $val == '') {
                echo "{$indent}<{$key}{$attr} />\n";
            } else {
                echo "{$indent}<{$key}{$attr}>{$val}</{$key}>\n";
            }
        }
    }
    // end while
}
Example #2
0
function array2xml(&$parent, $a)
{
    foreach ($a as $key => $val) {
        if (is_integer($key)) {
            $keystr = "record";
        } else {
            $keystr = $key;
        }
        if (is_object($val)) {
            $el = new DOMElement($keystr);
            $parent->appendChild($el);
            obj2xml($el, $val);
        } elseif (is_array($val)) {
            $el = new DOMElement($keystr);
            $parent->appendChild($el);
            array2xml($el, $val);
        } else {
            $parent->appendChild(new DOMElement($keystr, $val));
        }
    }
}
Example #3
0
function obj2xml($v, $indent = '')
{
    while (list($key, $val) = each($v)) {
        if ($key == '__attr') {
            continue;
        }
        // Check for __attr
        if (is_object($val->__attr)) {
            while (list($key2, $val2) = each($val->__attr)) {
                $attr .= " {$key2}=\"{$val2}\"";
            }
        } else {
            $attr = '';
        }
        if (is_array($val) || is_object($val)) {
            print "{$indent}<{$key}{$attr}>\n";
            obj2xml($val, $indent . '  ');
            print "{$indent}</{$key}>\n";
        } else {
            print "{$indent}<{$key}{$attr}>{$val}</{$key}>\n";
        }
    }
}