如何使用SAX解析器解析命名空间?

Sil*_*tri 7 java xml parsing android

使用Twitter搜索URL即.http://search.twitter.com/search.rss?q=android返回包含以下项目的CSS:

<item>
      <title>@UberTwiter still waiting for @ubertwitter  android app!!!</title>
      <link>http://twitter.com/meals69/statuses/21158076391</link>
      <description>still waiting for an app!!!</description>
      <pubDate>Sat, 14 Aug 2010 15:33:44 +0000</pubDate>
      <guid>http://twitter.com/meals69/statuses/21158076391</guid>
      <author>Some Twitter User</author>
      <media:content type="image/jpg" height="48" width="48" url="http://a1.twimg.com/profile_images/756343289/me2_normal.jpg"/>
      <google:image_link>http://a1.twimg.com/profile_images/756343289/me2_normal.jpg</google:image_link>
      <twitter:metadata>
        <twitter:result_type>recent</twitter:result_type>
</twitter:metadata>
</item>
Run Code Online (Sandbox Code Playgroud)

很简单.我的代码解析了所有内容(标题,链接,描述,pubDate等),没有任何问题.但是,我得到了null:

<google:image_link>
Run Code Online (Sandbox Code Playgroud)

我正在使用Java来解析RSS提要.我是否必须以不同于更简单的本地名称的方式处理复合本地名称?

这是解析Link,Description,pubDate等的一些代码:

@Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
        super.endElement(uri, localName, name);
        if (this.currentMessage != null){
            if (localName.equalsIgnoreCase(TITLE)){
                currentMessage.setTitle(builder.toString());
            } else if (localName.equalsIgnoreCase(LINK)){
                currentMessage.setLink(builder.toString());
            } else if (localName.equalsIgnoreCase(DESCRIPTION)){
                currentMessage.setDescription(builder.toString());
            } else if (localName.equalsIgnoreCase(PUB_DATE)){
                currentMessage.setDate(builder.toString());
            } else if (localName.equalsIgnoreCase(GUID)){
                currentMessage.setGuid(builder.toString());
            } else if (uri.equalsIgnoreCase(AVATAR)){
                currentMessage.setAvatar(builder.toString());
            } else if (localName.equalsIgnoreCase(ITEM)){
                messages.add(currentMessage);
            } 
            builder.setLength(0);   
        }
    }
Run Code Online (Sandbox Code Playgroud)

startDocument看起来像:

@Override
    public void startDocument() throws SAXException {
        super.startDocument();
        messages = new ArrayList<Message>();
        builder = new StringBuilder();

    }
Run Code Online (Sandbox Code Playgroud)

startElement看起来像:

@Override
    public void startElement(String uri, String localName, String name,
            Attributes attributes) throws SAXException {
        super.startElement(uri, localName, name, attributes);
        if (localName.equalsIgnoreCase(ITEM)){
            this.currentMessage = new Message();
        } 
    }
Run Code Online (Sandbox Code Playgroud)

托尼

pol*_*nts 1

像这样的元素具有属于命名空间的<google:image_link>本地名称。您需要确保 XML 解析框架能够识别命名空间,然后您需要使用适当的命名空间来查找该元素。image_linkgoogle

例如, 中的一些 SAX1 接口package org.xml.sax已被弃用,取而代之的是包含名称空间支持的 SAX2 对应接口(例如, SAX1Parser已弃用并被 SAX2 取代XMLReader)。uri请参阅有关如何指定命名空间或限定(前缀)的文档qName

也可以看看