Welcome Guest, Not a member yet? Register   Sign In
Simple XML helpers?
#1

[eluser]Unknown[/eluser]
I was working on some simple XML functions today. I'm pulling data out of a database and wanted to easily be able to mash up some XML with that data, so I came up with the following:

Code:
<?php

function to_xml($elements, $prefix='', $first=TRUE) {
    /* Simple converter that takes an associative array and turns it into
    an XML string. */
    $xml = '';
    if ($first) {
        /* Add the XML doctype at the top. */
        $xml .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
    }
    
    if (isset($elements['@value'])) {
        $attributes = '';
        foreach ($elements as $attribute => $value) {
            if ($attribute == '@key' || $attribute == '@value')
                continue;
            $attributes .= sprintf(" %s=\"%s\"",
            $attribute,
            $value);
        }
        $k = $elements['@key'];
        $v = $elements['@value'];
        if (is_array($v)) {
            /*
            <xml k="v">
                <something />
            &lt;/xml&gt; */
            $t = "\n{$prefix}<%s%s>%s\n{$prefix}</%s>";
            $xml .= sprintf($t,
            $k,
            $attributes,
            to_xml($v, $prefix."\t", FALSE),
            $k);
        } else {
            if ($v != '') {
                /* &lt;xml k="v"&gt;something&lt;/xml&gt; */
                $t = "\n{$prefix}<%s%s>%s</%s>";
                $xml .= sprintf($t,
                $k,
                $attributes,
                $v,
                $k);
            } else {
                /* &lt;xml k="v" /&gt; */
                $t = "\n{$prefix}<%s%s />";
                $xml .= sprintf($t,
                $k,
                $attributes);
            }
        }
    } else {
        foreach ($elements as $element) {
            $xml .= to_xml($element, $prefix, FALSE);
        }
    }
    
    return $xml;
}

?&gt;

Basically, this code takes an associative array and turns it into a pretty XML string. It's extremely basic and lacks some important functionality, but I'm posting this here because it might be useful for someone.

Here's an example of its usage:

Code:
&lt;?php

$input = array(
    array(
        '@key' => 'steam',
        '@value' => array(
            array(
                '@key' => 'success',
                '@value' => '1',
            ),
            array(
                '@key' => 'result',
                '@value' => array(
                    array(
                        '@key' => 'code',
                        '@value' => '4',
                    ),
                    array(
                        '@key' => 'message',
                        '@value' => 'Query was executed successfully, but no results were returned.',
                    ),
                ),
                'id' => '14',
            ),
            array(
                '@key' => 'feedback',
                '@value' => '',
            ),
        ),
    ),
);

header('Content-type: text/xml');
print to_xml($input);
/* Prints the following:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
<steam>
    <success>1</success>
    <result id="14">
        <code>4</code>
        <message>Query was executed successfully, but no results were returned.</message>
    </result>
    <feedback />
</steam> */
exit(0);

?&gt;

I think this would make a nice helper. It's not a full solution for all your XML needs, but it's a simple and handy solution in some cases.
#2

[eluser]got 2 doodle[/eluser]
Excellent, I haven't tried it yet but I will use this!

Thanks,
doodle

Welcome aboard!
#3

[eluser]RaZoR LeGaCy[/eluser]
You can look into using Zend_Feed
I merged two xml data arrays in to one.
#4

[eluser]Colin Williams[/eluser]
And what's wrong with using SimpleXML? http://us2.php.net/simplexml
#5

[eluser]Unknown[/eluser]
[quote author="Colin Williams" date="1214534773"]And what's wrong with using SimpleXML? http://us2.php.net/simplexml[/quote]Well, nothing, really, but this is even simpler. You could think of this as SimplerXML.




Theme © iAndrew 2016 - Forum software by © MyBB