duanju8431 2013-03-20 21:25
浏览 17
已采纳

解析Google Reader导出

I am attempting to use SimpleXML to parse the xml file that is produced from the Google Reader export.

File example:

<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
    <head>
        <title>TheTechBox subscriptions in Google Reader</title>
    </head>
    <body>
        <outline text="Engadget RSS Feed" title="Engadget RSS Feed"
            type="rss" xmlUrl="http://www.engadget.com/rss.xml" htmlUrl="http://www.engadget.com"/>
        <outline text="xkcd.com" title="xkcd.com" type="rss"
            xmlUrl="http://xkcd.com/rss.xml" htmlUrl="http://xkcd.com/"/>
    </body>
</opml>

This is what I have tried so far, the user uploads the file to this form and the form needs to loop through and extract the data.

<?php
if ($_FILES["file"]["error"] > 0)
{
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{

    $import = new SimpleXMLElement($_FILES["file"]["name"]);

    foreach($import->opml->body->outline[0] as $feed){
        echo $feed["title"];
    }

}
?>

Right now chrome produces a server error which indicates something is very wrong on the page, the file uploads OK so it appears to be the simpleXML part.

I am planning to do something more complex with the data later, I am currently trying to get it just to echo the data on the page (for demo purposes).

Any feedback would be greatly appreciated.

Here is the working code in the end

$import = simplexml_load_file($_FILES["file"]["tmp_name"]);

foreach($import->body->outline as $feed){
    echo $feed["title"];
}

This will need adding to to parse all of the data but this works.

  • 写回答

1条回答 默认 最新

  • dongzhankou2090 2013-03-20 21:52
    关注

    You've fallen into a classic trap when using SimpleXML: the first object you get when parsing a file or string is not an abstract "document" object, but the parent node. In this case, the parent node is <opml> ... </opml>, so $import in your sample code is that <opml> node. In other words, rather than $import->opml->body you just need to say $import->body.

    There's another bug in your loop as well, which is that you are asking for the first <outline> element (->outline[0]) when what you want is to be looping over all of the elements (foreach( $whatever->outline as $feed )).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面