使用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>
很简单我的代码解析了所有内容(标题,链接,描述,pubDate等),没有任何问题。但是,我在以下方面得到了空值:
<google:image_link>
我正在使用Java解析RSS feed。与更简单的本地名称相比,我对复合本地名称的处理方式是否有所不同?
这是解析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);
}
}
startDocument看起来像:
@Override
public void startDocument() throws SAXException {
super.startDocument();
messages = new ArrayList<Message>();
builder = new StringBuilder();
}
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();
}
}
托尼
最佳答案
像<google:image_link>
这样的元素的本地名称image_link
属于google
命名空间。您需要确保XML解析框架知道 namespace ,然后需要使用适当的 namespace 查找此元素。
例如, package org.xml.sax
中的一些SAX1接口(interface)已被弃用,由包含 namespace 支持的SAX2对应接口(interface)取代(例如,不建议使用SAX1 Parser
并由SAX2 XMLReader
替代)。请查阅有关如何指定 namespace uri
或限定(前缀)qName
的文档。
也可以看看
package org.xml.sax