Unable to represent properly formed XML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
denonth
Forum Newbie
Posts: 10
Joined: Tue Jan 10, 2012 1:26 pm

Unable to represent properly formed XML

Post by denonth »

Hi all I have code that is at other users seems to be working but for me not...
This is code, so can somone please try it and tell me what am I doing wrong because I am getting this output: MarkTwainCharlesDickens

And I should get this

Code: Select all

<?xml version="1.0"?>
<books>
 <book>
  <authorFirst>Mark</authorFirst>
  <authorLast>Twain</authorLast>
  <title>The Innocents Abroad</title>
 </book>
 <book>
  <authorFirst>Charles</authorFirst>
  <authorLast>Dickens</authorLast>
  <title>Oliver Twist</title>
 </book>
</books>
This is my code:

Code: Select all

class ArrayToXML
{
    /**
     * The main function for converting to an XML document.
     * Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
     *
     * @param array $data
     * @param string $rootNodeName - what you want the root node to be - defaultsto data.
     * @param SimpleXMLElement $xml - should only be used recursively
     * @return string XML
     */
    public static function toXml($data, $rootNodeName = 'data', &$xml=null)
    {
        // turn off compatibility mode as simple xml throws a wobbly if you don't.
        if (ini_get('zend.ze1_compatibility_mode') == 1)
        {
            ini_set ('zend.ze1_compatibility_mode', 0);
        }

        if (is_null($xml))
        {
            $xml = simplexml_load_string("<".key($data)."s/>");
        }

        // loop through the data passed in.
        foreach($data as $key => $value)
        {
            // if numeric key, assume array of rootNodeName elements
            if (is_numeric($key))
            {
                $key = $rootNodeName;
            }

            // delete any char not allowed in XML element names
            $key = preg_replace('/[^a-z0-9\-\_\.\:]/i', '', $key);

            // if there is another array found recrusively call this function
            if (is_array($value))
            {
                // create a new node unless this is an array of elements
                $node = ArrayToXML::isAssoc($value) ? $xml->addChild($key) : $xml;

                // recrusive call - pass $key as the new rootNodeName
                ArrayToXML::toXml($value, $key, $node);
            }
            else
            {
                // add single node.
                $value = htmlentities($value);
                $xml->addChild($key,$value);
            }

        }
        // pass back as string. or simple xml object if you want!
        return $xml->asXML();
    }

    // determine if a variable is an associative array
    public static function isAssoc( $array ) {
        return (is_array($array) && 0 !== count(array_diff_key($array, array_keys(array_keys($array)))));
    }
}
And the calling method file:

Code: Select all

require_once('test_xml.php');
$library = array(
    'book' => array(
        array(
            'authorFirst' => 'Mark',
            'authorLast' => 'Twain',
            'title' => 'The Innocents Abroad'
        ),
        array(
            'authorFirst' => 'Charles',
            'authorLast' => 'Dickens',
            'title' => 'Oliver Twist'
        )
    )
);
$ArrayToXml=new ArrayToXml();
echo $ArrayToXml->toXml($library);
Any idea why am I not getting right output. And others seems to do?

Thank you,
Denonth
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Unable to represent properly formed XML

Post by Celauran »

There's nothing wrong with your code. It's creating the XML file exactly as expected.
Post Reply