peipst9lker Posted July 23, 2012 Share Posted July 23, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/266110-serialization-of-simplexmlelement/ Share on other sites More sharing options...
peipst9lker Posted July 23, 2012 Author Share Posted July 23, 2012 I found a solution myself! A Class inside my framwork now handles the XML-parsing and writes the values into members. Now I can just serialize my own class and save the data. @Moderators, please close the thread Quote Link to comment https://forums.phpfreaks.com/topic/266110-serialization-of-simplexmlelement/#findComment-1363661 Share on other sites More sharing options...
xyph Posted July 23, 2012 Share Posted July 23, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266110-serialization-of-simplexmlelement/#findComment-1363688 Share on other sites More sharing options...
peipst9lker Posted July 24, 2012 Author Share Posted July 24, 2012 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). Quote Link to comment https://forums.phpfreaks.com/topic/266110-serialization-of-simplexmlelement/#findComment-1363932 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.