AndroidSaxラッパークラスのデバッグにかなりの時間を費やしています。私の質問は2つあります。1. Android Saxラッパークラスを使用してどのようにデバッグしますか?2.ここで何が間違っているのですか?
正常に動作する基本的なRSSリーダーがあります。Atom形式のリーダーを作成しようとしています。私は次のコードを持っています。
private List<Message> parseAtom()
{
final Message currentMessage = new Message();
final List<Message> messages = new ArrayList<Message>();
RootElement root = new RootElement("http://www.w3.org/2005/Atom", ATOM);
Element title = root.getChild(TITLE);
Element item = root.getChild(ENTRY);
title.setEndTextElementListener(new EndTextElementListener() {
public void end(String body)
{
Log.d("title", body);
}
});
item.setEndElementListener(new EndElementListener() {
public void end()
{
if (currentMessage.getAttribution() == null || currentMessage.getAttribution().length() == 0) currentMessage.setAttribution(Attribution);
messages.add(currentMessage.copy());
}
});
item.getChild(TITLE).setEndTextElementListener(new EndTextElementListener() {
public void end(String body)
{
currentMessage.setTitle(body);
}
});
item.getChild(ID).setEndTextElementListener(new EndTextElementListener() {
public void end(String body)
{
currentMessage.setLink(body);
}
});
item.getChild(SUMMARY).setEndTextElementListener(new EndTextElementListener() {
public void end(String body)
{
currentMessage.setDescription(body);
}
});
item.getChild(UPDATED).setEndTextElementListener(new EndTextElementListener() {
public void end(String body)
{
currentMessage.setDate(body);
}
});
item.getChild(Author).getChild(NAME).setEndTextElementListener(new EndTextElementListener() {
public void end(String body)
{
currentMessage.setAttribution(body);
}
});
try
{
String content = this.getInput();
Xml.parse(content, root.getContentHandler());
//Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return messages;
}
呼び出しの定数はgetChild()
、基本クラスで次のように定義されています。
static final String CHANNEL = "channel";
static final String PUB_DATE = "pubDate";
static final String DESCRIPTION = "description";
static final String LINK = "link";
static final String TITLE = "title";
static final String ITEM = "item";
static final String Author = "author";
static final String ENTRY = "entry";
static final String SUMMARY = "summary";
static final String ID = "id";
static final String UPDATED = "updated";
static final String NAME = "name";
例として使用したフィードは次のとおりです。
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
<title>NFL.com</title>
<link rel="alternate" href="http://www.nfl.com/rss/rsslanding" />
<link rel="self" href="http://www.nfl.com/rss/rsslanding" />
<subtitle>Latest news from NFL.com for Atlanta Falcons</subtitle>
<id>http://www.nfl.com/rss/rsslanding</id>
<rights>© 2012 NFL Enterprises LLC</rights>
<updated>2012-09-13T03:01:04Z</updated>
<dc:date>2012-09-13T03:01:04Z</dc:date>
<dc:rights>© 2012 NFL Enterprises LLC</dc:rights>
<entry>
<title>Brent Grimes to injured reserve with Achilles injury</title>
<link rel="alternate" href="http://www.nfl.com/goto?id=0ap1000000060448" />
<author>
<name>Hanzus, Dan</name>
</author>
<id>http://www.nfl.com/goto?id=0ap1000000060448</id>
<updated>2012-09-11T00:38:35Z</updated>
<published>2012-09-10T18:50:00Z</published>
<summary type="text">The Atlanta Falcons suffered a potentially devastating blow Monday with the news that cornerback Brent Grimes has played his last game of 2012. Who will step up in his absence?</summary>
<dc:creator>Hanzus, Dan</dc:creator>
<dc:date>2012-09-10T18:50:00Z</dc:date>
</entry>
</feed>
これが実行されると、Xml.parse(content, root.getContentHandler());
何もしていないように見えずに戻ります。例外はスローされません。ただし、いずれもListeners
ヒットしません(ブレークポイントで確認済み)。パーサーはlogcatに何もスローしていません。
ルート要素にを追加しtitle
て、それをヒットできるかどうかを確認しましたが、失敗しました。
私はもともと、名前空間が存在しないためにパーサーがエラーをスローするという問題を抱えていました(これはxmlドキュメントからfeed
与えられたものと一致していましたhttp://www.w3.org/2005/Atom:feed
...原因となるエラー)。名前空間を追加した後、エラーはスローされなくなりましたが、何も実行されていないようです。
これは、SAXラッパー(またはサックス)を使用する最初の試みです。私は何か簡単なものが欠けていますか?