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

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