0
$test_array = array (
                    'bla' => 'blub',
                    'foo' => 'bar',
                    'another_array' => array (
                        'stack' => 'overflow',
                    ),
                );
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($test_array, array ($xml, 'addChild'));
$xmldata =$xml->asXML();

But its showing

fatal error:SimpleXMLElement' not found.

I used same thing in Magento 1.9 and it worked that time.

1

Try using a forward slash before the SimpleXMLElement like

$xml = new \SimpleXMLElement('<root/>');

or you can try

function arrayToXml($array, $rootElement = null, $xml = null) {
  $_xml = $xml;

  if ($_xml === null) {
    $_xml = new SimpleXMLElement($rootElement !== null ? $rootElement : '<root/>');
  }

  foreach ($array as $k => $v) {
    if (is_array($v)) { //nested array
      arrayToXml($v, $k, $_xml->addChild($k));
    } else {
      $_xml->addChild($k, $v);
    }
  }

  return $_xml->asXML();
}
0

FIX: I believe you need to alias 'SimpleXMLElement' before using it. Just write it after namespace : Use SimpleXMLElement and it should work.

Magento Solution: I would like to mention that magento has already extended Simplexml which you can use. Check the class

Magento\Framework\Simplexml\Element

This class Extends SimpleXML to add valuable functionality to \SimpleXMLElement. Also make sure to use it by dependency in your custom class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.