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

Source for file unserializeRDF.php

Documentation is available at unserializeRDF.php

  1. <?PHP
  2. /**
  3.  * This example shows how to create any object
  4.  * from an XML document. In this case we get
  5.  * some aggregated objects for channel and items
  6.  * from an RSS feed.
  7.  *
  8.  * @author  Stephan Schmidt <schst@php.net>
  9.  */
  10.     error_reporting(E_ALL);
  11.  
  12.     require_once '../Unserializer.php';
  13.  
  14.    /**
  15.     * class for the RDF docuemnt
  16.     *
  17.     *
  18.     */
  19.     class rdfDocument
  20.     {
  21.         var $channel;
  22.         var $item;
  23.  
  24.         function getItems($amount)
  25.         {
  26.             return array_splice($this->item,0,$amount);
  27.         }
  28.     }
  29.  
  30.  
  31.    /**
  32.     * class that is used for a channel in the RSS file
  33.     *
  34.     * you could implement whatever you like in this class,
  35.     * properties will be set from the XML document
  36.     */
  37.     class channel
  38.     {
  39.         function getTitle()
  40.         {
  41.             return  $this->title;
  42.         }
  43.     }
  44.     
  45.    /**
  46.     * class that is used for an item in the RSS file
  47.     *
  48.     * you could implement whatever you like in this class,
  49.     * properties will be set from the XML document
  50.     */
  51.     class item
  52.     {
  53.         function getTitle()
  54.         {
  55.             return  $this->title;
  56.         }
  57.     }
  58.  
  59.  
  60.     $options = array(
  61.                      "complexType" => "object",
  62.                      "tagMap"      => array(
  63.                                                 "rdf:RDF"   => "rdfDocument",   // this is used to specify a classname for the root tag
  64.                                             )
  65.                     );
  66.     
  67.     //  be careful to always use the ampersand in front of the new operator 
  68.     $unserializer &new XML_Unserializer($options);
  69.  
  70.     $status $unserializer->unserialize("http://pear.php.net/feeds/latest.rss",true);    
  71.  
  72.     if (PEAR::isError($status)) {
  73.         echo    "Error: " $status->getMessage();
  74.     else {
  75.         $rss $unserializer->getUnserializedData();
  76.  
  77.         echo "This has been returned by XML_Unserializer:<br>";
  78.         
  79.         echo "<pre>";
  80.         print_r$rss );
  81.         echo "</pre>";
  82.  
  83.         echo "<br><br>Root Tagname: ".$unserializer->getRootName()."<br>";
  84.         
  85.         echo "Title of the channel: ".$rss->channel->getTitle()."<br>";
  86.  
  87.         $items $rss->getItems(3);
  88.         echo "<br>Titles of the last three releases:<br>";
  89.         foreach ($items as $item{
  90.             echo "Title : ".$item->getTitle()."<br>";
  91.         }
  92.     }
  93. ?>

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