android xml解析

本文介绍了一个用于解析CNBlog新闻的Java类库,该库利用SAX解析器处理XML格式的数据,提取每条新闻的ID、标题、摘要、发布时间、浏览次数、评论数量及来源等关键信息,并将这些信息封装到News对象中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.anjoyo.cnblogs.parse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import com.anjoyo.cnblogs.bean.News;

public class ParseNews extends DefaultHandler{
 private List<News> newsList = null;
 private News news = null;
 private String preTag = null;
 private boolean isNews = false;
 
 public List<News> getNewsList(String data) {
  SAXParserFactory factory = SAXParserFactory.newInstance();
  SAXParser parser = null;
  try {
   parser = factory.newSAXParser();
   parser.parse(data, this);
  } catch (ParserConfigurationException e) {
   e.printStackTrace();
  } catch (SAXException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  Log.i("Tag", newsList.size() + "");
  return this.getNewsList();
 }
 
 public List<News> getNewsList() {
  return this.newsList;
 }
 
 public void startDocument() throws SAXException {
  super.startDocument();
  newsList = new ArrayList<News>();
 }
 
 public void startElement(String uri, String localName, String qName,
   Attributes attributes) throws SAXException {
  super.startElement(uri, localName, qName, attributes);
  if(qName != null && qName.equals("entry")) {
   news = new News();
   isNews = true;
  }
  preTag = qName;
 }
 
 public void characters(char[] ch, int start, int length)
   throws SAXException {
  super.characters(ch, start, length);
  String content = new String(ch,start,length);
  Log.i("Tag", preTag);
  if(preTag != null && isNews) {
   if("id".equals(preTag)) {
    news.setId(content);
   }else if("title".equals(preTag)) {
    Log.i("Tag", content);
    news.setTitle(content);
   }else if("summary".equals(preTag)) {
    Log.i("Tag", content);
    news.setSummary(content);
   }else if("published".equals(preTag)) {
    news.setTime(content);
   }else if("views".equals(preTag)) {
    news.setViewNum(Integer.parseInt(content));
   }else if("comments".equals(preTag)) {
    news.setCommentNum(Integer.parseInt(content));
   }else if("sourceName".equals(preTag)) {
    news.setSource(content);
   }
  }
 }
 
 public void endElement(String uri, String localName, String qName)
   throws SAXException {
  super.endElement(uri, localName, qName);
  if(qName != null && qName.equals("entry")) {
   newsList.add(news);
   news = null;
   isNews = false;
  }
  preTag = null;
 }
 
 public void endDocument() throws SAXException {
  super.endDocument();
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值