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

Source for file serializeWithTagMap.php

Documentation is available at serializeWithTagMap.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.     error_reporting(E_ALL);
  15.     require_once 'XML/Serializer.php';
  16.  
  17.     $options = array(
  18.                         'indent'             => '    ',
  19.                         'linebreak'          => "\n",
  20.                         'mode'               => 'simplexml',
  21.                         'rootName'             => 'items'
  22.                     );
  23.  
  24.     $data = array(
  25.                     'item' => arrayarray(
  26.                                         'title'       => 'Foobar!',
  27.                                         'description' => 'This is some text....',
  28.                                         'link'        => 'http://foobar.com'
  29.                                     ),
  30.                                     array(
  31.                                         'title'       => 'Foobar2!',
  32.                                         'description' => ,
  33.                                         'link'        => 'http://foobar.com'
  34.                                     )
  35.                                 )
  36.                                                         
  37.                 );
  38.                     
  39.     
  40.     $serializer = new XML_Serializer($options);
  41.     
  42.     $result $serializer->serialize($data);
  43.     
  44.     if$result === true {
  45.         $xml $serializer->getSerializedData();
  46.  
  47.         echo    '<pre>';
  48.         print_rhtmlspecialchars($xml) );
  49.         echo    '</pre>';
  50.     else {
  51.         echo    '<pre>';
  52.         print_r($result);
  53.         echo    '</pre>';
  54.     }
  55.  
  56.     $newOptions = array(
  57.                         'rootName' => 'body',
  58.                         'replaceEntities' => XML_SERIALIZER_ENTITIES_HTML,
  59.                         'tagMap'   => array(
  60.                                               'item'        => 'div',
  61.                                               'title'       => 'h1',
  62.                                               'description' => 'p',
  63.                                               'link'        => 'tt'
  64.                                           )
  65.                     );
  66.     
  67.     $result $serializer->serialize($data$newOptions);
  68.     
  69.     if$result === true {
  70.         $xml $serializer->getSerializedData();
  71.  
  72.         echo    '<pre>';
  73.         print_rhtmlspecialchars($xml) );
  74.         echo    '</pre>';
  75.     else {
  76.         echo    '<pre>';
  77.         print_r($result);
  78.         echo    '</pre>';
  79.     }
  80.  
  81. ?>

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