douzuanze0486 2013-05-06 16:08
浏览 35
已采纳

PHP解析文本和描述SimpleXML

I'm trying to parse the text and description field from the code provided:

<?
$news = simplexml_load_file('http://en.wikipedia.org/w/api.php?action=opensearch&search=mandarin%20comics&format=xml&limit=1');

foreach($news->section->item as $item) {
    echo "<strong>" . $item->text . "</strong><br />";
    echo strip_tags($item->description) ."<br /><br />";
}
?>

It's giving me this error:

Invalid argument supplied for foreach() on line 4

How can I fix this?

Var_dump:

object(SimpleXMLElement)#1 (3) { ["@attributes"]=> array(1) { ["version"]=> string(3) "2.0" } ["Query"]=> string(15) "mandarin comics" ["Section"]=> object(SimpleXMLElement)#3 (1) { ["Item"]=> object(SimpleXMLElement)#2 (3) { ["Text"]=> string(17) "Mandarin (comics)" ["Description"]=> string(109) "The Mandarin is a fictional character, a supervillain who appears in comic books published by Marvel Comics. " ["Url"]=> string(46) "http://en.wikipedia.org/wiki/Mandarin_(comics)" } } }

Here is a link to the xml page: http://en.wikipedia.org/w/api.php?action=opensearch&search=mandarin%20comics&format=xml&limit=1

  • 写回答

2条回答 默认 最新

  • duanchi8112 2013-05-06 16:13
    关注

    XML is case sensitive, so:

    foreach($news->Section->Item as $item) {
        echo "<strong>" . $item->Text . "</strong><br />";
        echo strip_tags($item->Description) ."<br /><br />";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色