我正在调试 Android Sax 包装类。我的问题是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 包装器(或 sax)。我错过了一些简单的东西吗?