android 使用DOM解析xml

美国地震信息网http://earthquake.usgs.gov/earthquakes/catalogs/1day-M2.5.xml

public class EarthquakeList extends Activity {

ListView list;
EarthQuakeInfo selectedQuake;

ArrayAdapter<EarthQuakeInfo> adapter;
ArrayList<EarthQuakeInfo> infoList = new ArrayList<EarthQuakeInfo>();

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

setContentView(R.layout.main); //设置用户界面

list = (ListView)this.findViewById(R.id.list);

//设置ListView的内容为infoList
adapter = new ArrayAdapter<EarthQuakeInfo>(this,android.R.layout.simple_list_item_1,infoList);
//设置ListView的适配器为adapter
list.setAdapter(adapter);

getInfo(); //获得infoList的具体内容,这样ListView才有地震信息可以显示
}

/** 刷新数据,获得最新地址信息 */
private void getInfo() {
// 获得XML
URL url;
try {
String feed = getString(R.string.feed);
url = new URL(feed);

URLConnection connection = url.openConnection();

HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();

if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = httpConnection.getInputStream();

DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbfactory.newDocumentBuilder();

// 解析地震feed
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();

// 清空旧的地震信息
infoList.clear();

// 获得地震信息的列表
NodeList nl = docEle.getElementsByTagName("entry");
if (nl != null && nl.getLength() > 0) {
for (int i = 0 ; i < nl.getLength(); i++) {
Element entry = (Element)nl.item(i);
Element title = (Element)entry.getElementsByTagName("title").item(0);
Element geo = (Element)entry.getElementsByTagName("georss:point").item(0);
Element when = (Element)entry.getElementsByTagName("updated").item(0);

String details = title.getFirstChild().getNodeValue();
String point = geo.getFirstChild().getNodeValue();
String date = when.getFirstChild().getNodeValue();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
Date qdate = new GregorianCalendar(0,0,0).getTime();
try {
qdate = sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}

String[] location = point.split(" ");
Location loc = new Location("dummyGPS");
loc.setLatitude(Double.parseDouble(location[0]));
loc.setLongitude(Double.parseDouble(location[1]));

String magnitudeString = details.split(" ")[1];
int end = magnitudeString.length()-1;
double magnitude = Double.parseDouble(magnitudeString.substring(0, end));

details = details.split(",")[1].trim();

EarthQuakeInfo info = new EarthQuakeInfo(qdate, details, loc, magnitude);

// 处理新的地震信息
newEntry(info);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}

//新的地震信息
private void newEntry(EarthQuakeInfo info) {
// 将新的地址信息条目加入列表中
infoList.add(info);

// 通知array adapter
adapter.notifyDataSetChanged();
}
//地震信息类
public class EarthQuakeInfo {
public Date date;
public String details;
public Location location;
public double magnitude;

public EarthQuakeInfo(Date d, String de, Location loc, double mag) {
date = d;
details = de;
location = loc;
magnitude = mag;
}

@Override
public String toString() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd' 'hh:mm:ss");
return sdf.format(date)
+ "\n 里氏"
+ magnitude
+ "级 \n "
+ details;
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值