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.     error_reporting(E_ALL);
  13.  
  14.     require_once 'XML/Serializer.php';
  15.  
  16.     $options = array(
  17.                         "indent"             => "    ",
  18.                         "linebreak"          => "\n",
  19.                         "defaultTagName"     => "unnamedItem",
  20.                         "scalarAsAttributes" => true,
  21.                     );
  22.     
  23.     // this is just to get a nested object
  24.     $pearError = PEAR::raiseError('This is just an error object',123);
  25.     
  26.     $foo    =   new stdClass;
  27.     
  28.     $foo->value = "My value";
  29.     $foo->error = $pearError;
  30.     $foo->xml   = "cool";
  31.     
  32.     $serializer = new XML_Serializer($options);
  33.     
  34.     $result $serializer->serialize($foo);
  35.     
  36.     if$result === true {
  37.         $xml $serializer->getSerializedData();
  38.  
  39.         echo    "<pre>";
  40.         print_rhtmlspecialchars($xml) );
  41.         echo    "</pre>";
  42.     else {
  43.         echo    "<pre>";
  44.         print_r($result);
  45.         echo    "</pre>";
  46.     }
  47.  
  48. ?>

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