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

Source for file unserializeAnyXML.php

Documentation is available at unserializeAnyXML.php

  1. <?PHP
  2. /**
  3.  * This example shows different methods how
  4.  * XML_Unserializer can be used to create data structures
  5.  * from XML documents.
  6.  *
  7.  * @author  Stephan Schmidt <schst@php.net>
  8.  */
  9.  
  10.     // this is a simple XML document
  11.     $xml '<users>' .
  12.            '  <user handle="schst">Stephan Schmidt</user>' .
  13.            '  <user handle="mj">Martin Jansen</user>' .
  14.            '</users>';
  15.  
  16.     require_once 'XML/Unserializer.php';
  17.  
  18.     // complex structures are arrays, the key is the attribute 'handle'
  19.     $options = array(
  20.                      "complexType" => "array",
  21.                      "keyAttribute" => "handle"
  22.                     );
  23.  
  24.     //  be careful to always use the ampersand in front of the new operator 
  25.     $unserializer &new XML_Unserializer($options);
  26.  
  27.     // userialize the document
  28.     $status $unserializer->unserialize($xmlfalse);    
  29.  
  30.     if (PEAR::isError($status)) {
  31.         echo    "Error: " $status->getMessage();
  32.     else {
  33.         $data $unserializer->getUnserializedData();
  34.  
  35.         echo    "<pre>";
  36.         print_r$data );
  37.         echo    "</pre>";
  38.     }
  39.  
  40.  
  41.     // unserialize it again and change the complexType option
  42.     // but leave other options untouched
  43.     // now complex types will be an object, and the property name will be in the
  44.     // attribute 'handle'
  45.     $status $unserializer->unserialize($xmlfalsearray("complexType" => "object"));    
  46.  
  47.     if (PEAR::isError($status)) {
  48.         echo    "Error: " $status->getMessage();
  49.     else {
  50.         $data $unserializer->getUnserializedData();
  51.  
  52.         echo    "<pre>";
  53.         print_r$data );
  54.         echo    "</pre>";
  55.     }
  56.  
  57.  
  58.     // unserialize it again and change the complexType option
  59.     // and reset all other options
  60.     // Now, there's no key so the tags are stored in an array
  61.     $status $unserializer->unserialize($xmlfalsearray("overrideOptions" => true"complexType" => "object"));    
  62.  
  63.     if (PEAR::isError($status)) {
  64.         echo    "Error: " $status->getMessage();
  65.     else {
  66.         $data $unserializer->getUnserializedData();
  67.  
  68.         echo    "<pre>";
  69.         print_r$data );
  70.         echo    "</pre>";
  71.     }
  72.  
  73. ?>

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