XML_Serializer
[ class tree: XML_Serializer ] [ index: XML_Serializer ] [ all elements ]

Source for file unserializeObject.php

Documentation is available at unserializeObject.php

  1. <?PHP
  2. /**
  3.  * This example shows how XML_Serializer and XML_Unserializer
  4.  * work together.
  5.  *
  6.  * A structure is serialized and later it's unserialized from the
  7.  * resulting XML document.
  8.  *
  9.  * @author  Stephan Schmidt <schst@php.net>
  10.  */
  11.  
  12. require_once 'XML/Serializer.php';
  13. require_once 'XML/Unserializer.php';
  14. // this is just to get a nested object
  15. $pearError = PEAR::raiseError('This is just an error object',123);
  16.  
  17. $options = array(
  18.                     XML_SERIALIZER_OPTION_INDENT      => '    ',
  19.                     XML_SERIALIZER_OPTION_LINEBREAKS  => "\n",
  20.                     XML_SERIALIZER_OPTION_DEFAULT_TAG => 'unnamedItem',
  21.                     XML_SERIALIZER_OPTION_TYPEHINTS   => true
  22.                 );
  23.  
  24. $foo = new stdClass();
  25. $foo->value = 'My value';
  26. $foo->error = $pearError;
  27. $foo->xml   = array('This is' => 'cool');
  28. $foo->resource = fopen(__FILE__'r');
  29.  
  30. $serializer = new XML_Serializer($options);
  31. $result $serializer->serialize($foo);
  32.  
  33. if ($result === true{
  34.     $xml $serializer->getSerializedData();
  35. }
  36.  
  37. echo '<pre>';
  38. echo htmlspecialchars($xml);
  39. echo '</pre>';
  40.  
  41. //  be careful to always use the ampersand in front of the new operator 
  42. $unserializer &new XML_Unserializer();
  43.  
  44. $status $unserializer->unserialize($xml);    
  45.  
  46. if (PEAR::isError($status)) {
  47.     echo 'Error: ' $status->getMessage();
  48. else {
  49.     $data $unserializer->getUnserializedData();
  50.     echo '<pre>';
  51.     print_r($data);
  52.     echo '</pre>';
  53. }
  54. ?>

Documentation generated on Mon, 11 Mar 2019 15:41:41 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.