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

Source for file serializeWithAttributes.php

Documentation is available at serializeWithAttributes.php

  1. <?PHP
  2. /**
  3.  * XML Serializer example
  4.  *
  5.  * This example demonstrates, how XML_Serializer is able
  6.  * to serialize scalar values as an attribute instead of a nested tag.
  7.  *
  8.  * The same structure like in serializeObject.php is serialized.
  9.  *
  10.  * @author  Stephan Schmidt <schst@php.net>
  11.  */
  12.  
  13. require_once 'XML/Serializer.php';
  14.  
  15. $options = array(
  16.                     XML_SERIALIZER_OPTION_INDENT               => '    ',
  17.                     XML_SERIALIZER_OPTION_LINEBREAKS           => "\n",
  18.                     XML_SERIALIZER_OPTION_DEFAULT_TAG          => 'unnamedItem',
  19.                     XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES => true,
  20.                 );
  21.  
  22. // this is just to get a nested object
  23. $pearError = PEAR::raiseError('This is just an error object',123);
  24.  
  25. $foo    =   new stdClass;
  26.  
  27. $foo->value = 'My value';
  28. $foo->error = $pearError;
  29. $foo->xml   = 'cool';
  30.  
  31. $serializer = new XML_Serializer($options);
  32.  
  33. $result $serializer->serialize($foo);
  34.  
  35. if ($result === true{
  36.     $xml $serializer->getSerializedData();
  37.  
  38.     echo '<pre>';
  39.     echo htmlspecialchars($xml);
  40.     echo '</pre>';
  41. else {
  42.     echo '<pre>';
  43.     print_r($result);
  44.     echo '</pre>';
  45. }
  46. ?>

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