create xml file from php array with SimpleXMLElement

79 Views Asked by At

I´m trying to create a complete XML file with laravel and SimpleXMLElement.

I´m reading that i can to build this file from associative array and i´m trying it. I need create this xml to send this file to send it API.

But my problem it´s, when i try build my file, return this message:

Call to undefined function App\Classes\array_to_xml()

to create my file, i build a personaliced class that contain any method to build logics. I´m calling to my class, so:

$logalty = new Logalty();
$xml_user_info = new \SimpleXMLElement('<?xml version="1.0"?><user_info></user_info>');
$logalty->array_to_xml($allData, $xml_user_info);

And when i´m calling my function, return previous message.

In my class Logalty() i have my function array_to_xml() that contain:

public function array_to_xml($data, &$xml_user_info)
        {
            foreach($data as $key => $value) {
                if(is_array($value)) {
                    if(!is_numeric($key)){
                        $subnode = $xml_user_info->addChild("$key");
                        array_to_xml($value, $subnode);
                    }else{
                        $subnode = $xml_user_info->addChild("item$key");
                        array_to_xml($value, $subnode);
                    }
                }else {
                    $xml_user_info->addChild("$key",htmlspecialchars("$value"));
                }
            }

            //creating object of SimpleXMLElement
            $xml_user_info = new \SimpleXMLElement("<?xml version=\"1.0\"?><user_info></user_info>");

            //function call to convert array to xml
            array_to_xml($data, $xml_user_info);

            //saving generated xml file
            $xml_file = $xml_user_info->asXML('users.xml');

            //success and error message based on xml creation
            if($xml_file){
                echo 'XML file have been generated successfully.';
            }else{
                echo 'XML file generation error.';
            }

        }

I don´t know why return this message. I try instead of call to array_to_xml, call $this->array_to_xml(), but delaey so much. it´s my first time with xml in php and i don´t know very well.

Thanks for readme and sorry for my bad english

1

There are 1 best solutions below

0
scorpions77 On

Finally, i resolve my problem with this code:

first in my controller i change my call to my class, instead of:

$logalty = new Logalty();
$logalty->array_to_xml($allData, $rootElement = null, $xml = null, $path);

And in my class logalty()

public function array_to_xml($array, $rootElement = null, $xml = null, $path)
        {
            $_xml = $xml;
     
            // If there is no Root Element then insert root
            if ($_xml === null) {
                //$_xml = new \SimpleXMLElement($rootElement !== null ? $rootElement : '<root/>');
                $_xml = new \SimpleXMLElement('<?xml version="1.0"?><installation_info></installation_info>');
            }
            
            // Visit all key value pair
            foreach ($array as $k => $v) {
                // If there is nested array then
                if (is_array($v)) { 
                    // Call function for nested array
                    $this->array_to_xml($v, $k, $_xml->addChild($k));
                }else {
                    // Simply add child element. 
                    $_xml->addChild($k, $v);
                }
            }
            
            return $_xml->asXML($path.'/installation.xml');

This return my xml, and save in my folder. My inital problem it´s that i was doing a recursive function with no sense.