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

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