Android 为什么Sax解析器找不到任何元素?
我在调试AndroidSAX包装器类时花了很多时间。我的问题有两个方面。1.如何使用Android Sax包装类进行调试?2.我做错了什么 我有一个基本的RSS阅读器,工作正常。我正在尝试创建一个Atom格式的阅读器。我有以下代码Android 为什么Sax解析器找不到任何元素?,android,sax,saxparser,Android,Sax,Saxparser,我在调试AndroidSAX包装器类时花了很多时间。我的问题有两个方面。1.如何使用Android Sax包装类进行调试?2.我做错了什么 我有一个基本的RSS阅读器,工作正常。我正在尝试创建一个Atom格式的阅读器。我有以下代码 private List<Message> parseAtom() { final Message currentMessage = new Message(); final List<Message&
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;
}
我使用的提要作为示例如下:
<?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>
NFL.com
亚特兰大猎鹰队NFL.com最新消息
http://www.nfl.com/rss/rsslanding
©2012年NFL企业有限责任公司
2012-09-13T03:01:04Z
2012-09-13T03:01:04Z
©2012年NFL企业有限责任公司
布伦特·格里姆斯因跟腱受伤前往受伤的预备队
韩祖,丹
http://www.nfl.com/goto?id=0ap1000000060448
2012-09-11T00:38:35Z
2012-09-10T18:50:00Z
亚特兰大猎鹰队周一遭受了一次潜在的毁灭性打击,因为有消息称,角后卫布伦特·格里姆斯已经参加了2012年的最后一场比赛。他不在的时候谁来接替?
韩祖,丹
2012-09-10T18:50:00Z
运行时,Xml.parse(content,root.getContentHandler())代码>返回,但不显示要执行任何操作。它不会抛出异常。但是没有一个侦听器被命中(通过断点验证)。解析器没有在logcat中抛出任何内容
我在根元素上添加了标题
,看看我是否可以点击它,但没有点击
我最初遇到一个问题,解析器抛出一个错误,因为名称空间不存在(匹配feed
给定的http://www.w3.org/2005/Atom:feed
来自xml文档…导致错误)。添加名称空间后,它不再抛出错误,但似乎什么也没做
这是我第一次尝试使用SAX包装器(或SAX包装器)。我是否遗漏了一些简单的内容?最近,当我尝试使用此方法解析一些XML时,遇到了同样的问题,我花了一些时间才弄清楚,但问题是我在子查找中遗漏了名称空间。所以在你的读者中
Element title = root.getChild(TITLE);
使用
每次在阅读器中使用getChild()
时,您都必须这样做。太好了!谢谢你的帮助。
Element title = root.getChild(TITLE);
Element title = root.getChild(NAMESPACE, TITLE);