954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Parsing a RSS feed from another webpage

Hi,
I am using a PHP parser to extract some contents from a RSS feed.
The parser used to work just fine, but when I changed my hosting the parser stopped working and now it shows the following error:

XML Error: not well-formed (invalid token) at line 11

The XML file its a valid RSS feed according to feedvalidator.org and I just can't find the reason why it used to work and now it doesn't. Does anybody know why it is not working in this new hosting?? or if a possible reason for this?

RSS feed: http://www.banxico.org.mx/rsscb/rss?BMXC_canal=fix&BMXC_idioma=es

Thanks!

julio gomez
Newbie Poster
13 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

It appears that it is not accepting the special characters. Perhaps because the document is ISO8859. Try converting it to UTF8.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 
It appears that it is not accepting the special characters. Perhaps because the document is ISO8859. Try converting it to UTF8.


Thanks for the suggestion, the problem is that I kind of new with php and the parser I have was made by someone else and I'm not sure how to do that, here is the code for the parser including the utf-8:

class RSSParser {

   var $insideitem = false;
   var $tag = "";
   var $valor = "";
	//Function to use at the start of an element
   function start($parser, $tagName, $attrs) {
	   if ($this->insideitem) {
		   $this->tag = $tagName;
	   } elseif ($tagName == "CB:EXCHANGERATE") {
		   $this->insideitem = true;
	   }
   }

	//Function to use at the end of an element
   function stop($parser, $tagName) {
	   if ($tagName == "CB:EXCHANGERATE") {
		   $this->insideitem = false;
	   }
   }
	
	//Function to use when finding character data
   function char($parser, $data) {
	   if ($this->insideitem) {
		   switch ($this->tag) {
			   case "CB:VALUE":
			   $this->valor .= $data;
			   break;
		   }
	   }
   }
} 
	//Initialize the XML parser
$xml_parser=xml_parser_create(); 
$rss_parser = new RSSParser(); 
xml_set_object($xml_parser,&$rss_parser);
	
	//Setting UTF-8
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); 

	//Specify element handler
xml_set_element_handler($xml_parser,"start","stop");

	//Specify data handler
xml_set_character_data_handler($xml_parser,"char");

	//Open XML file
$fp=fopen("http://www.banxico.org.mx/rsscb/rss?BMXC_canal=fix&BMXC_idioma=es","r");

	//Read 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)));
  }

	//Free the XML parser
xml_parser_free($xml_parser);


I am using the xml_parser_set_option() to change to utf-8 but I am not sure if that is correct.

Any help would be appreciated, thanks

julio gomez
Newbie Poster
13 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

I am facing the same problem I was searching and landed here but it's unfortunate that no one has helped with the issue :(

farneville
Newbie Poster
1 post since Apr 2010
Reputation Points: 10
Solved Threads: 1
 

Perhaps you can change the string that contains the xml with http://php.net/manual/en/function.utf8-encode.php this function. Note that before parsing it, you would need to change the xml encoding in the header too.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Thanks for the help, I tried that before but it didn't work and I don't know if I am doing it correctly.
I am not downloading the whole XML, just a single line.
Considering this how do you suggest using the utf8_encode function in my code??

Thanks!!

julio gomez
Newbie Poster
13 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

Something like this perhaps.

xml_parse($xml_parser,utf8_encode($data),feof($fp)) or ..
pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Something like this perhaps.

xml_parse($xml_parser,utf8_encode($data),feof($fp)) or ..


VERY simple solution.
I tried using utf8_encode($data) before that line and it didn't work. Putting it as you suggest does the trick! thanks!!!

julio gomez
Newbie Poster
13 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: