开发者

Quickly convert simpleXMLObject to STDClass?

开发者 https://www.devze.com 2022-12-09 07:03 出处:网络
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

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.


$my_std_class = json_decode(json_encode($my_simplexmlelement));
$my_assoc_array = json_decode(json_encode($my_simplexmlelement), true);


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.


Another way is:

(object)(array)$my_simplexmlelement

Unfortunately if you have children they remain as SimpleXMLElement


I don't know if there's a way to convert the object without iterating through it. My guess is that you can't.

You can check this thread out, it shows you how to convert a SimpleXML to an array, you can adapt that.

0

精彩评论

暂无评论...
验证码 换一张
取 消