Jump to content

Problem with parsing XML


ivytony

Recommended Posts

I'm trying to parse an XML file outputed by Metacafe.com API: http://www.metacafe.com/api/item/2348336

 

Below is my code:

 

<?php

class RSSParser {

   var $insideitem = false;
   var $tag = "";
   var $title = "";
   var $description = "";
   var $link = "";
   //var $video_details

   function startElement($parser, $tagName, $attrs) {
       if ($this->insideitem) {
           $this->tag = $tagName;
       } elseif ($tagName == "ITEM") {
           $this->insideitem = true;
       }
   }

   function endElement($parser, $tagName) {
   public $video_details = array();

       if ($tagName == "ITEM") {
       
   /*
       printf("<p><b><a href='%s'>%s</a></b></p>",
           trim($this->link),htmlspecialchars(trim($this->title)));
           printf("<p>%s</p>",
           htmlspecialchars(trim($this->description)));
   */
	$video_details["title"] = $this->title;
	$video_details["link"] = $this->link;
	$video_details["description"] = $this->description;
   		
	   $this->title = "";
           $this->description = "";
           $this->link = "";
           $this->insideitem = false;
       
   return $video_details;
   }
   }

   function characterData($parser, $data) {
       if ($this->insideitem) {
           switch ($this->tag) {
               case "TITLE":
               $this->title .= $data;
               break;
               case "DESCRIPTION":
               $this->description .= $data;
               break;
               case "LINK":
               $this->link .= $data;
               break;
           }
       }
   }
}

$xml_parser = xml_parser_create();
$rss_parser = new RSSParser();
xml_set_object($xml_parser,&$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://www.metacafe.com/api/item/2348336","r")
    or die("Error reading RSS data.");
while ($data = fread($fp, 4096))
   xml_parse($xml_parser, $data, feof($fp))
       or die(sprintf("XML error: %s at line %d",  
           xml_error_string(xml_get_error_code($xml_parser)),  
           xml_get_current_line_number($xml_parser)));

echo $video_details["title"];

fclose($fp);
xml_parser_free($xml_parser);

?>

 

But when I run it, I got a blank page. I wonder how to return the array $video_details in the function endElement. Or somewhere might be wrong?

Link to comment
https://forums.phpfreaks.com/topic/142894-problem-with-parsing-xml/
Share on other sites

the code seams quite old

Try this

its not a full RSS but you could be able to pull the data you require

<?php
$feedUrl = 'http://www.metacafe.com/api/item/2348336';
$rawFeed = file_get_contents($feedUrl);
$xml = new SimpleXmlElement($rawFeed);
$channel['title'] = (string)$xml->channel->title;
$channel['link']  = (string)$xml->channel->link;
$article = array();
foreach ($xml->channel->item as $item)
{     
    $article['title'] = (string)$item->title;
    $article['link'] = (string)$item->link;
}
echo $article['title'];
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.