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.            '  <group name="qa">PEAR QA Team</group>' .
  16.            '  <foo id="test">This is handled by the default keyAttribute</foo>' .
  17.            '  <foo id="test2">Another foo tag</foo>' .
  18.            '</users>';
  19.  
  20.     require_once 'XML/Unserializer.php';
  21.  
  22.     // complex structures are arrays, the key is the attribute 'handle' or 'name', if handle is not present
  23.     $options = array(
  24.                      "complexType" => "array",
  25.                      "keyAttribute" => array(
  26.                                               'user'  => 'handle',
  27.                                               'group' => 'name',
  28.                                               '__default' => 'id'
  29.                                             )
  30.                     );
  31.  
  32.     //  be careful to always use the ampersand in front of the new operator 
  33.     $unserializer &new XML_Unserializer($options);
  34.  
  35.     // userialize the document
  36.     $status $unserializer->unserialize($xmlfalse);    
  37.  
  38.     if (PEAR::isError($status)) {
  39.         echo    "Error: " $status->getMessage();
  40.     else {
  41.         $data $unserializer->getUnserializedData();
  42.  
  43.         echo    "<pre>";
  44.         print_r$data );
  45.         echo    "</pre>";
  46.     }
  47.  
  48.  
  49.     // unserialize it again and change the complexType option
  50.     // but leave other options untouched
  51.     // now complex types will be an object, and the property name will be in the
  52.     // attribute 'handle'
  53.     $status $unserializer->unserialize($xmlfalsearray("complexType" => "object"));    
  54.  
  55.     if (PEAR::isError($status)) {
  56.         echo    "Error: " $status->getMessage();
  57.     else {
  58.         $data $unserializer->getUnserializedData();
  59.  
  60.         echo    "<pre>";
  61.         print_r$data );
  62.         echo    "</pre>";
  63.     }
  64.  
  65.  
  66.     // unserialize it again and change the complexType option
  67.     // and reset all other options
  68.     // Now, there's no key so the tags are stored in an array
  69.     $status $unserializer->unserialize($xmlfalsearray("overrideOptions" => true"complexType" => "object"));    
  70.  
  71.     if (PEAR::isError($status)) {
  72.         echo    "Error: " $status->getMessage();
  73.     else {
  74.         $data $unserializer->getUnserializedData();
  75.  
  76.         echo    "<pre>";
  77.         print_r$data );
  78.         echo    "</pre>";
  79.     }
  80.  
  81. ?>

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