home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 88 / PIWD88.iso / mac / contents / developer / tutorial_files / Pages92-93 / parser.php
PHP Script  |  2003-10-30  |  2KB  |  175 lines

  1. <?
  2.  
  3.  
  4.  
  5. $channel = array();
  6.  
  7.  
  8.  
  9. function opening_element($parser, $element, $attributes) {
  10.  
  11.     global $c, $i, $flag, $type;
  12.  
  13.     switch ($element) {
  14.  
  15.         case "channel":
  16.  
  17.             $type = $element;
  18.  
  19.             $channel[$c]["items"] = array();
  20.  
  21.             break;
  22.  
  23.         case "item":
  24.  
  25.             $type = $element;
  26.  
  27.             $channel[$c]["items"][$i] = array();
  28.  
  29.             break;
  30.  
  31.         case "image":
  32.  
  33.             $type = $element;
  34.  
  35.             $channel[$c]["image"] = array();
  36.  
  37.             break;
  38.  
  39.         default:
  40.  
  41.             $flag = $element;
  42.  
  43.             break; } }
  44.  
  45.  
  46.  
  47. function closing_element($parser, $element) {
  48.  
  49.     global $c, $i, $flag, $type;
  50.  
  51.     switch ($element) {
  52.  
  53.         case "channel":
  54.  
  55.             $type = '';
  56.  
  57.             $c++;
  58.  
  59.             break;
  60.  
  61.         case "item":
  62.  
  63.             $type = '';
  64.  
  65.             $i++; 
  66.  
  67.             break;
  68.  
  69.         case "image":
  70.  
  71.             $type = ''; 
  72.  
  73.             break;
  74.  
  75.         default:
  76.  
  77.             $flag = '';
  78.  
  79.             break; } }
  80.  
  81.  
  82.  
  83. function character_data($parser, $data) {
  84.  
  85.     global $i, $channel, $flag, $type;
  86.  
  87.     switch ($type) {
  88.  
  89.         case "channel":
  90.  
  91.             $channel[$c][$flag] = $data;
  92.  
  93.             break;
  94.  
  95.         case "item":
  96.  
  97.             $channel[$c]["items"][$i][$flag] = $data;
  98.  
  99.             break;
  100.  
  101.         case "image":
  102.  
  103.             $channel[$c]["image"][$flag] = $data;
  104.  
  105.             break; } }
  106.  
  107.  
  108.  
  109. $parser = xml_parser_create();
  110.  
  111. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
  112.  
  113. xml_set_element_handler($parser, 'opening_element', 'closing_element');
  114.  
  115. xml_set_character_data_handler($parser, 'character_data');
  116.  
  117. $document = file('http://quiddity/articles/RSS/feed.rss');
  118.  
  119. foreach ($document as $line) {
  120.  
  121.     xml_parse($parser, $line); }
  122.  
  123. xml_parser_free($parser);
  124.  
  125.  
  126.  
  127. foreach ($channel as $thisChannel) {
  128.  
  129.     echo "
  130.  
  131.         <html><head><title>".$thisChannel["title"]."</title></head>
  132.  
  133.         <body>";
  134.  
  135.         
  136.  
  137.     if ($thisChannel["image"]) {
  138.  
  139.         echo "<img src='".$thisChannel["image"]["url"]."' alt='".$thisChannel["image"]["title"]."' width='".$thisChannel["image"]["width"]."' height='".$thisChannel["image"]["height"]."'    >"; }
  140.  
  141.  
  142.  
  143.     echo "
  144.  
  145.         <p><a href='".$thisChannel["link"]."'>".$thisChannel["title"]."</a></p>
  146.  
  147.         <p>".$thisChannel["description"]."</p>
  148.  
  149.         <p align=center><table width=80%>
  150.  
  151.         ";
  152.  
  153.  
  154.  
  155.     foreach ($thisChannel["items"] as $thisItem) {
  156.  
  157.         echo "
  158.  
  159.             <tr><td><a href='".$thisItem["link"]."'>".$thisItem["title"]."</a>
  160.  
  161.             <br>".$thisItem["description"]."</td></tr>
  162.  
  163.             "; }
  164.  
  165.     
  166.  
  167.     echo "
  168.  
  169.         </table></p>
  170.  
  171.         </body></html>"; }
  172.  
  173.         
  174.  
  175. ?>