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

Source for file serializeWithIndentedAttributes.php

Documentation is available at serializeWithIndentedAttributes.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.  * In this example tags with more than one attribute become
  9.  * multiline tags, as each attribute gets written to a
  10.  * separate line as 'indentAttributes' is set to '_auto'.
  11.  *
  12.  * @author  Stephan Schmidt <schst@php.net>
  13.  */
  14.     require_once 'XML/Serializer.php';
  15.  
  16.     $options = array(
  17.                         "indent"             => "    ",
  18.                         "linebreak"          => "\n",
  19.                         "typeHints"          => false,
  20.                         "defaultTagName"     => "unnamedItem",
  21.                         "scalarAsAttributes" => true,
  22.                         "indentAttributes"   => "_auto"
  23.                     );
  24.     
  25.     // this is just to get a nested object
  26.     $pearError = PEAR::raiseError('This is just an error object',123);
  27.     
  28.     $foo    =   new stdClass;
  29.     
  30.     $foo->value = "My value";
  31.     $foo->error = $pearError;
  32.     $foo->xml   = "cool";
  33.     
  34.     $serializer = new XML_Serializer($options);
  35.     
  36.     $result $serializer->serialize($foo);
  37.     
  38.     if$result === true {
  39.         $xml $serializer->getSerializedData();
  40.  
  41.         echo    "<pre>";
  42.         print_rhtmlspecialchars($xml) );
  43.         echo    "</pre>";
  44.     else {
  45.         echo    "<pre>";
  46.         print_r($result);
  47.         echo    "</pre>";
  48.     }
  49.  
  50. ?>

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