gpt4 book ai didi

blackberry - 如何解析 RSS 提要并将其显示为黑莓应用程序中的链接?

转载 作者:行者123 更新时间:2023-12-03 09:33:12 25 4
gpt4 key购买 nike

我想解析 xml 提要并在我的黑莓应用程序中显示为链接。

谷歌搜索后,我发现我必须使用 SAX 解析器。我还没有找到任何好的例子。

例如,如果我想解析来自 bbc.co.uk 的新闻 rss 提要。怎么做。如何从 rss 提要中提取图像。

请帮助、建议和指导我。新航谢谢

最佳答案

假设我们正在谈论它的 twitter rss xml:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"
xmlns:georss="http://www.georss.org/georss">
<channel>
<title>Twitter / LPProjekt</title>
<link>http://twitter.com/LPProjekt</link>
<atom:link type="application/rss+xml"
href="http://twitter.com/statuses/user_timeline/27756405.rss" rel="self"/>
<description>Twitter updates from Linkin Park Projekt</description>
<language>en-us</language>
<ttl>40</ttl>
<item>
<title>LPProjekt: the instrumental from &quot;what ive done&quot;</title>
<description>LPProjekt: the instrumental from &quot;</description>
<pubDate>Sun, 07 Feb 2010 23:34:26 +0000</pubDate>
<guid>http://twitter.com/LPProjekt/statuses/8784251683</guid>
<link>http://twitter.com/LPProjekt/statuses/8784251683</link>
</item>
..
</channel>
</rss>

首先为您的 xml 实现处理程序,仅从 获取 <title> 和 值:

class RSSHandler extends DefaultHandler {
boolean isItem = false;
boolean isTitle = false;
boolean isLink = false;
String[] title = new String[] {};
String[] link = new String[] {};
String value = "";

public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
if (!isItem) {
if (name.equalsIgnoreCase("item"))
isItem = true;
} else {
if (name.equalsIgnoreCase("title"))
isTitle = true;
if (name.equalsIgnoreCase("link"))
isLink = true;
}
}

public void characters(char[] ch, int start, int length)
throws SAXException {
if (isTitle || isLink)
value = value.concat(new String(ch, start, length));
}

public void endElement(String uri, String localName, String name)
throws SAXException {
if (isItem && name.equalsIgnoreCase("item"))
isItem = false;
if (isTitle && name.equalsIgnoreCase("title")) {
isTitle = false;
Arrays.add(title, value);
value = "";
}
if (isLink && name.equalsIgnoreCase("link")) {
isLink = false;
Arrays.add(link, value);
value = "";
}
}
}

现在,要使用它,请从 HttpConnection 获取 InputStream 并将其全部放入 SAXParser.parse 中:

static String[][] getURLFromRSS(String url) {
InputStream is = null;
HttpConnection connection = null;
RSSHandler rssHandler = new RSSHandler();
try {
connection = (HttpConnection) Connector.open(url);
is = connection.openInputStream();
try {
SAXParser parser = SAXParserFactory.newInstance()
.newSAXParser();
parser.parse(is, rssHandler);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
if (connection != null)
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String[][] result = new String[2][];
result[0] = rssHandler.title;
result[1] = rssHandler.link;
return result;
}

现在使用自定义 LabelField 作为链接字段:

class LinkLabel extends LabelField
{
String mUrl = "";
public LinkLabel(String title, String url) {
super(title, FOCUSABLE);
mUrl = url;
}
protected boolean navigationClick(int status, int time) {
Browser.getDefaultSession().displayPage(mUrl);
return true;
}
}

使用示例:

public Scr() {
String rssUrl = "http://twitter.com/statuses/user_timeline/27756405.rss";
String[][] urlData = getURLFromRSS(rssUrl);
for (int i = 0; i < urlData.length; i++) {
String title = urlData[0][i];
String url = urlData[1][i];
add(new LinkLabel(title, url));
}
}

alt text http://img215.imageshack.us/img215/7583/rssurl.jpg

关于blackberry - 如何解析 RSS 提要并将其显示为黑莓应用程序中的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2438619/

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