为什么Sax Parser没有找到任何元素?

时间:2012-09-13 05:54:34

标签: android sax saxparser

我正忙着调试Android Sax包装器类。我的问题是2折。 1.如何使用Android Sax包装器类进行调试?我在这里做错了什么?

我有一个基本的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,看看我是否可以点击它,但是没有去。

我最初遇到的问题是解析器因为命名空间不存在而抛出错误(匹配feed它从xml doc ... {error}中得到http://www.w3.org/2005/Atom:feed。添加命名空间后,它不再抛出错误,但似乎没有做任何事情。

这是我第一次尝试使用SAX包装器(或萨克斯)。我错过了一些简单的东西吗?

1 个答案:

答案 0 :(得分:2)

我最近遇到了同样的问题,当我尝试使用这种方法解析一些XML时,我花了一些时间来解决它,但问题是我在子查找中省略了命名空间。所以在你的读者而不是

Element title = root.getChild(TITLE);

使用

Element title = root.getChild(NAMESPACE, TITLE);

每次在阅读器中使用getChild()时,您都必须这样做。

相关问题