我正在尝试获取网站的内容并使其显示在 android 应用程序中,所以我按照教程使用 SAX 解析器来解析网站的 XML 内容,所以根据我创建 RSS HANDLER CLASS 的说明该类的功能是 SAX Parser 遍历整个 XML 文件,每当遇到开始标记时,就会调用 endElement 方法并为其分配 localname 变量,我在 getTitle()、getthumb 中遇到错误() 等,所以请任何人纠正我所做的错误.. 这是我的 RSS 处理程序代码..enter code here
类 RSSHandler 扩展 DefaultHandler {
private static final String PostList = null;
private Post currentPost = new Post();
StringBuffer chars = new StringBuffer();
@Override
public void startElement(String uri, String localName, String qName,
Attributes atts) {
chars = new StringBuffer();
if (localName.equalsIgnoreCase("item")) {
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (localName.equalsIgnoreCase("title")
&& currentPost.getTitle() == null) {
currentPost.setTitle(chars.toString());
}
if (localName.equalsIgnoreCase("pubDate")
&& currentPost.getPubDate() == null) {
currentPost.setPubDate(chars.toString());
}
if (localName.equalsIgnoreCase("thumbnail")
&& currentPost.getThumbnail() == null) {
currentPost.setThumbnail(chars.toString());
}
if (localName.equalsIgnoreCase("link")
&& currentPost.getUrl() == null) {
currentPost.setUrl(chars.toString());
}
if (localName.equalsIgnoreCase("item")) {
PostList.add(currentPost);
currentPost = new Post();
}