gpt4 book ai didi

android - 为 Android 解析 RSS 提要

转载 作者:太空宇宙 更新时间:2023-11-03 12:53:50 24 4
gpt4 key购买 nike

这是一个 RSS 提要示例。

http://na.leagueoflegends.com/en/rss.xml

<channel>
<title>Most Recent News</title>
<link>http://na.leagueoflegends.com/en/rss.xml</link>
<description/>
<language>en-US</language>
<item>
<title>Community-recommended bundles available now </title>
<link>http://na.leagueoflegends.com/en/news/store/sales/community-recommended-bundles-available-now?ref=rss</link>
<description>&lt;div class="field field-name-field-body-small field-type-text-long field-label-hidden">Craving something new at champ select? Get fed on these limited-time bundles based on players’ suggestions!&lt;/div>&lt;div class="field field-name-field-article-media field-type-file field-label-hidden">&lt;div id="file-11675" class="file file-image file-image-jpeg"> &lt;img typeof="foaf:Image" src="/sites/default/files/styles/large/public/upload/bundle-banner_1.jpg?itok=4MD_leb5" width="480" height="270" alt="" title="" />&lt;/div>&lt;/div>&lt;div class="field field-name-custom-author field-type-entityreference field-label-hidden">&lt;span class="article_detail">&lt;span class="posted_by">By WizardCrab&lt;/span>&lt;/span>&lt;/div></description>
<pubDate>Mon, 30 Jun 2014 19:35:11 +0000</pubDate>
<dc:creator>sbroome</dc:creator>
<guid isPermaLink="false">18268 at http://na.leagueoflegends.com</guid>
</item>
<item>
<title>Champion and skin sale: 07.04 - 07.07</title>
<link>http://na.leagueoflegends.com/en/news/store/sales/champion-and-skin-sale-0704-0707?ref=rss</link>
<description>&lt;div class="field field-name-field-body-small field-type-text-long field-label-hidden">Grab these champions and skins on sale for 50% off for a limited time:&lt;/div>&lt;div class="field field-name-field-article-media field-type-file field-label-hidden">&lt;div id="file-13873" class="file file-image file-image-jpeg"> &lt;img typeof="foaf:Image" src="/sites/default/files/styles/large/public/upload/mu842qnf.jpg?itok=OktrEQdQ" width="480" height="270" alt="" title="" />&lt;/div>&lt;/div>&lt;div class="field field-name-custom-author field-type-entityreference field-label-hidden">&lt;span class="article_detail">&lt;span class="posted_by">By WizardCrab&lt;/span>&lt;/span>&lt;/div></description>
<pubDate>Tue, 24 Jun 2014 21:24:10 +0000</pubDate>
<dc:creator>sbroome</dc:creator>
<guid isPermaLink="false">18060 at http://na.leagueoflegends.com</guid>
</item>
<item>
....
....
.... (and goes on for a while)

我想在 .

中获取 eat 属性

我正在使用

            URL rssFeedUrl = new URL(rssUrl);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
RSSHandler rssHandler = new RSSHandler();
xmlReader.setContentHandler(rssHandler);
InputSource inputSource = new InputSource(rssFeedUrl.openStream());
xmlReader.parse(inputSource);


private class RSSHandler extends DefaultHandler {

public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException {
if (localName.equals("item")) {
item = true;
}
if(item) {
if(localName.equals(RSS_TITLE)) {

} else if(localName.equals(RSS_LINK)) {

} else if(localName.equals(RSS_DESCRIPTION)) {

} else if(localName.equals(RSS_PUB_DATE)) {

} else if(localName.equals(RSS_CREATOR)) {

} else if(localName.equals(RSS_GUID)) {

}

}
if (!localName.equals("item") && item == true) {
rssResult = rssResult + "\n\n" + localName + ": ";
}


}

public void endElement(String namespaceURI, String localName, String qName) throws SAXException {

}

public void characters(char[] ch, int start, int length) throws SAXException {
String cdata = new String(ch, start, length);
if (item == true) {
String x = (cdata.trim()).replaceAll("\\s+", " ");
rssResult = rssResult + x + "\t";
}
}

有没有更简单的方法?因为现在太乏味了。我基本上想说 getElement("description")

最佳答案

正如 CommonsWare 提到的那样,如果您想保持简单,您可能应该为此使用第三方 RSS 库。 Android Arsenal 中最新的 RSS 库是我制作的:PkRSS https://github.com/Pkmmte/PkRSS

API 极其简单而灵活。

// Loads your RSS feed in a background thread
PkRSS.with(this).load(url).async();

// After it has finished loading, use this to get the result
List<Article> articleList = PkRSS.with(this).get(url);

当然,如果您访问该库的 GitHub 页面,还有更多详细信息。

关于android - 为 Android 解析 RSS 提要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24579701/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com