【问题标题】:Difficulty Reading with Atom Reader使用 Atom Reader 阅读困难
【发布时间】:2013-02-22 09:59:40
【问题描述】:

我尝试为 RSS 和 Atom 制作解析器,其中 RSS 提要显示标题,而 Atom 显示图像和描述以及链接。看来我的解析器只适用于 RSS。你能告诉我为什么吗?检查这个:

public void Get_Parse_Feed(String URL_link, Input_Streamer_Class is, List<String> headlines, List<String> links)
{
    try
    {
        // URL
        is = new Input_Streamer_Class();

        /*
         * 
         *             Reserved URLS:
         *             --> http://feeds.pcworld.com/pcworld/latestnews
         *             --> http://feeds2.feedburner.com/boy-kuripot
         *             --> http://feeds2.feedburner.com/phcreditcardpromos
         *             --> http://feeds.feedburner.com/blogspot/MKuf
         *             --> http://googleblog.blogspot.com/atom.xml
         * 
         */

        is.Set_URL(URL_link);

        // Set XML pull factory.
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(false);
        XmlPullParser xpp = factory.newPullParser();

        // Picking up input stream...
        xpp.setInput(is.Get_Input_Stream(is.Get_URL()), "UTF_8");

        // Check for inside item.
        boolean insideItem = false;

        // Pick event type. (START_TAG, END_TAG, etc.)
        int eventType = xpp.getEventType();
        while(eventType != XmlPullParser.END_DOCUMENT)
        {
            if(eventType == XmlPullParser.START_TAG)
            {
                if(xpp.getName().equalsIgnoreCase("item"))
                {
                    insideItem = true;

                } else if(xpp.getName().equalsIgnoreCase("title")) {

                    if(insideItem)
                    {
                        headlines.add(xpp.nextText()); // --> Extract the headline.
                    }

                } else if(xpp.getName().equalsIgnoreCase("link")) {

                    if(insideItem)
                    {
                        links.add(xpp.nextText()); // --> Extract the link of article.
                    }

                }

            } else if((eventType == XmlPullParser.END_TAG) && xpp.getName().equalsIgnoreCase("item")) {

                insideItem = false;

            }

            eventType = xpp.next(); // --> Move to the next element.
        }

    } catch(MalformedURLException e) {

        e.printStackTrace();

    } catch(XmlPullParserException e) {

        e.printStackTrace();

    } catch(IOException e) {

        e.printStackTrace();

    }
}

每当我找到本教程并为自己使用 MVC 进行管理时,结果都令人印象深刻。但是,当我尝试实现一个包含 Atom 提要的 URL 时,它没有显示出来。

【问题讨论】:

    标签: java android parsing feed atom-feed


    【解决方案1】:

    很明显结构不同,你需要2个不同的解析器!或者 2 个合二为一(真的不知道会有多大帮助)

    因为:

    RSS 2.0:

    <?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0">
            <channel>
    
                    <title>Example Feed</title>
                    <description>Insert witty or insightful remark here</description>
                    <link>http://example.org/</link>
                    <lastBuildDate>Sat, 13 Dec 2003 18:30:02 GMT</lastBuildDate>
                    <managingEditor>johndoe@example.com (John Doe)</managingEditor>
    
                    <item>
                            <title>Atom-Powered Robots Run Amok</title>
                            <link>http://example.org/2003/12/13/atom03</link>
                            <guid isPermaLink="false">urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</guid>
                            <pubDate>Sat, 13 Dec 2003 18:30:02 GMT</pubDate>
                            <description>Some text.</description>
                    </item>
    
            </channel>
    </rss>
    

    原子 1.0:

    <?xml version="1.0" encoding="utf-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom">
    
            <title>Example Feed</title>
            <subtitle>Insert witty or insightful remark here</subtitle>
            <link href="http://example.org/"/>
            <updated>2003-12-13T18:30:02Z</updated>
            <author>
                    <name>John Doe</name>
                    <email>johndoe@example.com</email>
            </author>
            <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
    
            <entry>
                    <title>Atom-Powered Robots Run Amok</title>
                    <link href="http://example.org/2003/12/13/atom03"/>
                    <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
                    <updated>2003-12-13T18:30:02Z</updated>
                    <summary>Some text.</summary>
            </entry>
    
    </feed>
    

    比较here,您的代码中的不同问题是在原子上您有entry 而不是item,就像在rss 中一样!

    【讨论】:

    • 但是,这两个代码用于包含提要的 HTML 网站:RSS 或 Atom。
    • 而且,如果我对您的答案的第一条评论有误,您将这些放在哪里?在 res 文件夹下的 layout 文件夹中?如何连接或如何工作?
    • 嗯...我明白了。我会尝试找出答案。
    • 您可以将行 if(xpp.getName().equalsIgnoreCase("item")) 更改为 if(xpp.getName().equalsIgnoreCase("item") || xpp.getName().equalsIgnoreCase("entry")) 但如果您想解析更多元素,最好考虑一种更复杂、更安全的方法。
    • 我正在使用这个包含 Atom 提要的 URL,http://googleblog.blogspot.com/atom.xml,但它仍然没有显示。我是否获得了包含 Atom 提要的正确 URL?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多