Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By leo.chen on
Hi All,
I am new to Drupal and PHP code, I found in Drupal 7, SimpleXMLElement is used like this:
$xml = new SimpleXMLElement($result);
return $this->xml2array($xml);
function xml2array($xmlObject, $out = array()) {
foreach ((array) $xmlObject as $index => $node) {
$out[$index] = (is_object($node)) ? $this->xml2array($node) : $node;
}
return $out;
}
And it can be used without any import "use" in the beginning of the php code, so there was a PHP Fatal error: Class 'Drupal\xxxx\SimpleXMLElement' not found error, while in Drupal 8, I added the import "use Symfony\Component\DependencyInjection\SimpleXMLElement;" and this error is gone, but the same code cannot work as as in Drupal 7. Can anyone advise how to use this object as in Drupal 7's code, or is there any other replacement for it?
Thanks in advance.
Comments
usage of SimpleXMLElement in Drupal 8
Hope this will be helpful to you
Step-1:- XMl String
Step-2:- For XMl Object
Step-3:- For array
Note:- SimpleXML seems to have problems with the colon ":" in the response tags, so take them out
using preg_replace().
Thanks, this works!
Thanks, this works!
Thank you
It worked for me. Thank you very much!
Useful. Thanks !!
Useful. Thanks !!