douque9982 2017-06-02 18:54
浏览 548
已采纳

难以解析XML文档“此XML文件似乎没有与之关联的任何样式信息。”

I've searched a lot over the past few days but i'm now resigning to asking for help.

I'm parsing a number of news websites using PHP's simplexml_load_file(), my code is below....

foreach ($sourceID as $s) {

$xml = simplexml_load_file($s['rss']);

$dateTimeArray = [];

if ($xml != false) {
foreach($xml->channel->item as $item)
{       
    foreach($item->pubDate as $date) {
        $dateTimeArray[] = (string) $date;
    }

    $s['dateTimeList'] = $dateTimeArray;        
}
array_push($source, $s);
} else {

echo 'error '.$s['id'];
}
}

There are some sites that have "This XML file does not appear to have any style information associated with it. The document tree is shown below." displayed at the top, this is something to do with not being correctly read as an XML document?

Ultimately my php code will not parse any data from the websites that display this message, any help that can be easily added to my present code would be greatly appreciated.

One of the xml feeds in question is this one https://cointelegraph.com/rss

Thanks!

  • 写回答

1条回答 默认 最新

  • douci1918 2017-06-11 10:07
    关注

    Fixed this by setting a specific browser:

    ini_set("user_agent","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Mobile Safari/537.36");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!