如何序列化/反序列化SimpleXML对象? [英] How-to Serialize/Unserialize a SimpleXML Object?

查看:141
本文介绍了如何序列化/反序列化SimpleXML对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一些

I've seen a few creative solutions for dealing with serialized SPL objects but am looking for more options (or elaborations). I store nested serialized objects - of which, one is SimpleXML - in the database, only to be un-serialized later. This obviously cause some problems.

$s = new SimpleXmlElement('<foo>bar</foo>');
$ss = serialize($s);
$su = unserialize($ss);
// Warning: unserialize() [function.unserialize]: Node no longer exists...

有人对用于处理序列化SPL对象的高度可靠的方法有任何见识吗? __sleep()/__ wakeup()是否覆盖?转换为stdClass?强制转换为字符串,然后序列化?

Does anyone have any insight into highly-reliable methods for dealing with serialized SPL objects? __sleep()/__wakeup() overrides? Cast-to-stdClass? Cast-to-string, then serialize?

感谢您的帮助.

[编辑":这些XML模式的范围和变化太大,无法与ORM进行映射.从最根本的意义上讲,它们是有状态的进程中的任意有效载荷,它们是在宁静的API中触发的.]

[Edit: The scope and variation of these XML schemas are too varied to map with an ORM. They are, at their most fundamental level, arbitrary payloads in stateful processes, triggered within restful APIs.]

推荐答案

尽管存在适当性问题,您仍可以将其转换为XML,如下所示:

Questions on appropriateness notwithstanding, you can turn it back into XML like this:

$xml = $simpleXmlElem->asXML();

然后,当您从数据库中将其提取时:

And then, when you pull it from the database:

$simpleXmlElem = simplexml_load_string($xml);


关于仅序列化大块XML是否合适,将XML放入数据库中确实消除了使用关系系统的许多优势,但是您确实具有能够容纳任意关系的优势.工作量.如果某些字段是通用字段,并且/或者您从适当地对其进行规范化中受益(例如,您希望基于这些字段进行选择),请将其移至规范化列中.


As for whether it's appropriate to just serialize large chunks of XML, it can be true that putting XML into the database removes much of the advantage of using a relational system, but you do have the advantage of being able to accommodate an arbitrary workload. If some fields are universal, and/or you gain benefit from normalizing them properly (e.g. you want to select based on those fields), move those into normalized columns.

这篇关于如何序列化/反序列化SimpleXML对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆