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

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