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

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