Monday, 30 August 2010

Symfony 'String could not be parsed as XML'

I was receiving this error on my symfony project (using the sfWhoIsOnline plugin):

[28-Aug-2010 21:50:54] PHP Fatal error:  Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/xxxxxxx/plugins/sfWhoIsOnlinePlugin/lib/sfWhoIsOnlineUserFacade.class.php:38
Stack trace:
#0 /home/xxxxxxx/plugins/sfWhoIsOnlinePlugin/lib/sfWhoIsOnlineUserFacade.class.php(38): SimpleXMLElement->__construct('')
#1 /home/xxxxxxx/plugins/sfWhoIsOnlinePlugin/lib/filter/sfWhoIsOnlineFilter.class.php(14): sfWhoIsOnlineUserFacade::registerUser(Object(myUser))
#2 /home/xxxxxxx/cache/frontend/prod/config/config_core_compile.yml.php(1026): sfWhoIsOnlineFilter->execute(Object(sfFilterChain))
#3 /home/xxxxxxx/plugins/sfDoctrineGuardPlugin/lib/sfGuardRememberMeFilter.class.php(56): sfFilterChain->execute()
#4 /home/xxxxxxx/cache/frontend/prod/config/config_core_compile.yml.php(1026): sfGuardRememberMeFilter->execute(Object(sfFilterChain))
#5 /home/xxxxxxx/cache/frontend/prod/config/config_core_compile.yml.php(990): sfFilterChain->execute()
#6 /home/xxxxxxx/cache/frontend/prod/config/config_core_compile.yml.php(1026): sfRenderingFilt in /home/xxxxxxx/plugins/sfWhoIsOnlinePlugin/lib/sfWhoIsOnlineUserFacade.class.php on line 38
[29-Aug-2010 00:06:48] String could not be parsed as XML
[29-Aug-2010 02:14:47] String could not be parsed as XML

Turns out, SimpleXMLElement can't handle empty strings or badly encoded documents well. As a patch of sorts, I replaced the code at line 38 in the sfWhoIsOnlineUserFacade class with the following lines (original lines in bold):

/* Begin Hack to fix error 'String could not be parsed as XML' */
try {
  $xml = new SimpleXMLElement($xmlString);    // <-- Original line 38   
$instance->fromXml($xml);                // <-- Original line 39
} catch (Exception $e) {
  sfContext::getInstance()->getLogger()->crit('sfWhoIsOnline failed to create SimpleXMLElement');
  sfContext::getInstance()->getLogger()->crit('xmlString: '.$xmlString);
}
/* End Hack */

References:
http://drupal.org/node/541892
http://weierophinney.net/matthew/archives/111-mbstring-comes-to-the-rescue.html
http://www.google.com/search?hl=en&q=String+could+not+be+parsed+as+XML&aq=f&aqi=&aql=&oq=&gs_rfai=
http://ketarin.canneverbe.com/forum/viewtopic.php?id=384

No comments: