データの一部を読み取って表示する必要がある RSS リーダー アプリケーションを開発しています。https://twitter.com/statuses/user_timeline/27756405.rssの URL から取得したデータからデータのみtitle
を抽出する必要があります。link
xml の解析で問題に直面しており、この問題について助けが必要です。
私のコードは以下に添付されています。
public class RssMain extends UiApplication {
RssMain theApp;
public static void main(String[] args) {
// create an instance of our app
RssMain theApp = new RssMain();
// "run" the app
theApp.enterEventDispatcher();
}
// app constructor
public RssMain() {
// create an instance of the main screen of our application
RssScreen screen = new RssScreen();
// make the screen visible
UiApplication.getUiApplication().pushScreen(screen);
}
class RssScreen extends MainScreen {
public RssScreen() {
String rssUrl = "http://twitter.com/statuses/user_timeline/27756405.rss";
String[][] urlData = RSSHandler.getURLFromRSS(rssUrl);
for (int i = 0; i < urlData.length; i++) {
String title = urlData[0][i];
String url = urlData[1][i];
System.out.println("TITLE " + title);
System.out.println("URL " + url);
}
}
}
}
RSSHandler の実装
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import net.rim.blackberry.api.browser.Browser;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.util.Arrays;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
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 = "";
}
}
public 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;
}
}