Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7076077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:15:17+00:00 2026-05-28T06:15:17+00:00

I am using this class for porting array to XML: class ArrayToXML { /**

  • 0

I am using this class for porting array to XML:

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("");
        }

        // 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 I am calling this function with this code sample just to test a bit:

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();
$ArrayToXml->toXml($library)

And I am getting this error: Fatal error: Call to a member function addChild() on a non-object in on line 42.

Any idea what has went wrong?
Thank you.

EDIT:
Line 42: $node = ArrayToXML::isAssoc($value) ? $xml->addChild($key) : $xml;

EDIT2:
With all do respect guys, this code is ain’t working yet. Answer was correct, but I only get MarkTwainCharlesDickens as an output. And I shoud get properly formed XML. Should I enable some extensions? But I have already seen that simplexml is enabled.

Any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:15:18+00:00Added an answer on May 28, 2026 at 6:15 am

    You cannot create an XML document out of an empty string.

    var_dump(simplexml_load_string(""));
    bool(false)
    

    vs

    var_dump(simplexml_load_string("<root/>"));
    object(SimpleXMLElement)#1 (0) {}
    

    To get the book value out of your $data:

    $root = key($data);
    $rootNode = "<{$root}/>";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this class to load multiple images synchronously. Somehow the loader doesn't
I tried to send an email using this class below, but no success, no
I'm using this php class ( http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php ) for creating the different sized images
I've embedded a SWF into a class using this syntax above my class definition:
Im using DataContractJsonSerializer to serealize this class: public class User { public string id
I have a class that I am using below. And I am using this
So we have our J2EE application using Log4j like this public class CustomerController {
I am using an Objective c class, a subclass of NSObject. This class cannot
I created a class under 'src' dir. I'm using this code to access the
In this I need C++ array class template, which is fixed-size, stack-based and doesn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.