php 如何序列化/反序列化 SimpleXML 对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2970957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:18:06  来源:igfitidea点击:

How-to Serialize/Unserialize a SimpleXML Object?

phpserializationobject

提问by allnightgrocery

I've seen a few creative solutionsfor 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.

我已经看到了一些处理序列化 SPL 对象的创造性解决方案,但我正在寻找更多选项(或详细说明)。我将嵌套的序列化对象(其中一个是 SimpleXML)存储在数据库中,稍后才将其取消序列化。这显然会导致一些问题。

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

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?

有没有人对处理序列化 SPL 对象的高度可靠方法有任何见解?__sleep()/__wakeup() 覆盖?投射到标准类?强制转换为字符串,然后序列化?

Any help is appreciated.

任何帮助表示赞赏。

[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 模式的范围和变化太多,无法与 ORM 映射。在最基本的层面上,它们是有状态进程中的任意有效负载,在 RESTful API 中触发。]

回答by Joseph Mastey

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

尽管存在关于适当性的问题,但您可以像这样将其转换回 XML:

$xml = $simpleXmlElem->asXML();

And then, when you pull it from the database:

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

$simpleXmlElem = simplexml_load_string($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.

至于仅序列化大块 XML 是否合适,将 XML 放入数据库确实会消除使用关系系统的大部分优势,但您确实具有能够适应任意工作负载的优势。如果某些字段是通用的,和/或您从正确规范化它们中受益(例如,您想根据这些字段进行选择),请将它们移到规范化列中。

回答by uNDERsCORE

More clear and OOP.

更清晰和OOP。

namespace MyApp;
class SimpleXMLElement extends \SimpleXMLElement
{
    public function arrayToXml($array = array())
    {
        array_walk_recursive($array, array(&$this, 'addChildInverted'));
        return $this;
    }

    public function addChildInverted($name ,$value)
    {
        parent::addChild($value,$name);
    }

}

and calling

并打电话

  $xml = new \MyApp\SimpleXMLElement('<resultado/>');
  echo $xml->arrayToXml($app->getReturnedValue())->asXML();

回答by Charles

Wouldn't simply rendering and storing the XML be the best way to serialize any object that represents an XML structure?

简单地呈现和存储 XML 难道不是序列化表示 XML 结构的任何对象的最佳方式吗?

What are you trying to do with the serialized data that might prevent this?

您想对可能阻止这种情况的序列化数据做什么?

edit:

编辑:

Also,

还,

I store nested serialized objects [...] in the database, only to be un-serialized later

我将嵌套的序列化对象 [...] 存储在数据库中,稍后才取消序列化

Why are you storing PHP serialized data in a database? There are many better ways to store objects in a database.

为什么要将 PHP 序列化数据存储在数据库中?有许多更好的方法可以在数据库中存储对象。