PHP errors parsing XML (RSS feed)

末鹿安然 提交于 2019-12-04 09:26:37

Well, it might not be pretty, compared to getting the feed fixed, but this is a solution:

    $xml_source = str_replace(array("&", "&"), array("&", "&"), file_get_contents($feed_uri));
    $x = simplexml_load_string($xml_source);

Firstly, I replace the & to ordinary & to make sure I convert ALL & back to & again.

The problem lies in the XML - specifically with the '&' character in the phrase '84th AHIMA Annual Convention & Exhibit' - this ought to be escaped. You can find out if there's a problem with any XML you're dealing with by putting it through any online XML validator such as http://www.xmlvalidation.com/.

Xman Classical

As mentionned in other answers and comments, your source XML is broken and XML parsers are supposed to reject invalid input. libxml has a "recover" mode which would let you load this broken XML, but you would lose the "&sid" part so it wouldn't help.

If you're lucky and you like taking chances, you can try to somehow make it work by kind-of-fixing the input. You can use some string replacement to escape the ampersands that look like they're in the query part of an URL.

    $xml = file_get_contents('broken.xml');
    // replace & followed by a bunch of letters, numbers
    // and underscores and an equal sign with &
    $xml = preg_replace('#&(?=[a-z_0-9]+=)#', '&', $xml);
    $sxe = simplexml_load_string($xml);

This is, of course, nothing but a hack and the only good way to fix your situation is to ask your XML provider to fix their generator. Because if it generates broken XML, who knows what other errors slip by unnoticed?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!