www.webdeveloper.com
Results 1 to 1 of 1

Thread: Testing to be sure my simpleXML object serialized and unserialized correctly.

  1. #1
    Join Date
    Oct 2007
    Posts
    370

    Testing to be sure my simpleXML object serialized and unserialized correctly.

    I am working with an API that returns XML. I use simplexml_load_string to convert it to an object.

    I then connect to a mysql DB and do:
    PHP Code:
    //$parsed is the object.
    $obj_for_DB=mysql_real_escape_string(serialize($parsed)); 
    I store that value as a BLOB.
    I pulled it out and process it like this:

    PHP Code:
    $SQL="SELECT * FROM test_table";

    $result=mysql_query($SQL);
    $row=mysql_fetch_assoc($result);
    $new_object=sxml_unserialze($row['object']);
    //I had to use this custom function because of an error unserializing simpleXML elements.
    //error like "SimpleXMLElement - Warning: unserialize() [function.unserialize]: Node no longer exists in  .php"

    //found on PHP.net. Work around for error on unserializing SimpleXML object.
    function sxml_unserialze($str)
    {
    return 
    unserialize(str_replace(array('O:16:"SimpleXMLElement":0:{}''O:16:"SimpleXMLElement":'), array('s:0:"";''O:8:"stdClass":'), $str));

    The problem I am having now is that $new_object is an instance of stdClass and I want to know if it is identical to $parsed (besides being a different class). Neither "==" or "===" is going to do it. I tried running both objects through get_object_vars and comparing the arrays, but that didn't seem to work either. When I print_r the two objects, they look the same except for the class name, but there's so much data that I'd like to find a way to programatically compare them at least once during my testing phase. Any ideas? Is there a way to unserialize my simpleXML object without converting it, or is there a way to compare all the properties and their values regarless of the difference in class?
    Last edited by TecBrat; 12-29-2011 at 02:07 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles