Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quickly convert simpleXMLObject to STDClass?

Tags:

Does anybody know a quick way to convert a SimpleXMLElement to a normal STDClass object, without iterating through each branch manually? I would feel better working with a normal object after fetching the data.

like image 594
Pekka Avatar asked Oct 18 '09 12:10

Pekka


2 Answers

$my_std_class = json_decode(json_encode($my_simplexmlelement)); $my_assoc_array = json_decode(json_encode($my_simplexmlelement), true); 
like image 160
Skyler Johnson Avatar answered Sep 19 '22 12:09

Skyler Johnson


I suggest looking into using XMLReader, which lends itself well to extracting data and storing it as whatever data type one wishes, instead of SimpleXML. It's especially good for regularly-used documents (I use it, extended as RSSReader, for RSS), is much faster than might be expected, and as a bonus uses less memory than SimpleXML.

like image 20
GZipp Avatar answered Sep 21 '22 12:09

GZipp