Jump to content

Serialization of SimpleXMLElement


peipst9lker

Recommended Posts

Hi, I'm having trouble serializing an Object of SimpleXMLElement but PHP throws an Exception.

 

Exception: Serialization of 'SimpleXMLElement' is not allowed

$xml = new SimpleXMLElement($input);
$ser = serialize($xml);
var_dump($ser);

 

Did some research about serializing objects and I found out that SimpleXMLElement does not have implemented the Serializable-class.

I have a large XML-file and don't want to parse it every time a user enters a certain page, my point is to keep the process as resource-efficient as possible.

Already tried to use json_decode(json_encode($xml)); but then attributes get lost (which are important).

If u have any idea please let me know!

Link to comment
Share on other sites

Well, your application still has to convert/parse the serialized class, so you might not saving any processing time.

 

You should try both methods, and benchmark. It wouldn't surprise me if storing the modified XML, and then re-parsing it on the next page was actually faster.

Link to comment
Share on other sites

Thanks for your reply xyph, benchmark results (using microtime() to get the exact time differences):

 

5000x Parsing xml-file: 1.3933548927307
5000x Unserializing cache-file: 0.56393098831177

 

Parsing serialized data is a lot quicker then parsing XML data, so parsing it once and saving serialized data once  is totally worth it :)

Of course u can only do it when the XML-file is always the same (which is, in my case).

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.