How-to Serialize/Unserialize a SimpleXML Object?
Asked Answered
P

3

14

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...

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?

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.]

Pori answered 4/6, 2010 at 1:30 Comment(0)
H
36

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);

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.

Hedvig answered 4/6, 2010 at 2:29 Comment(1)
The bigger question in my mind is why doesn't PHP just do this automatically? Is there a serious penalty in serializing/deserializing xml objects in PHP?Zwiebel
N
3

More clear and 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();
Nall answered 3/4, 2014 at 22:6 Comment(0)
D
1

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

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.

Denticulation answered 4/6, 2010 at 1:53 Comment(5)
"Why are you storing PHP serialized data in a database?" For storing the state of rather large dynamic processes. "Wouldn't simply rendering and storing the XML be the best way to serialize any object that represents an XML structure?" Possibly. That was the question I posed.Pori
"There are many better ways to store objects." Would you care to elaborate on this point?Pori
Sure. Try mapping each property in an object to a column in a table. If the property is itself an object, that object needs to be stored in another table, and can be referenced in the parent using a foreign key. This is normally called "object-relational mapping," and can get kind of complex. If you're using a key-value or document database, things are different of course...Denticulation
Sure, ORM. We have a complete ORM schema for the core elements but the more complicated elements are these processes (n-length and 100% dynamic). It would be virtually impossible to to scope an ORM schema to handle the extraordinary volume of objects, variation, and state-changes. Although, it's absolutely imperative to our core models. It's always nice to see solid structuring enter into a conversation about design.Pori
If it's at least well structured, maybe something like a document database may be better for storage and retrieval than just serializing it into a blob and sticking it in a table.Denticulation

© 2022 - 2024 — McMap. All rights reserved.