android 使用DOM解析xml

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

publicclassEarthquakeListextendsActivity{

ListViewlist;

EarthQuakeInfoselectedQuake;

ArrayAdapter<EarthQuakeInfo>adapter;

ArrayList<EarthQuakeInfo>infoList=newArrayList<EarthQuakeInfo>();

@Override

publicvoidonCreate(Bundleicicle){

super.onCreate(icicle);

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

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

//设置ListView的内容为infoList

adapter=newArrayAdapter<EarthQuakeInfo>(this,android.R.layout.simple_list_item_1,infoList);

//设置ListView的适配器为adapter

list.setAdapter(adapter);

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

}

/**刷新数据,获得最新地址信息*/

privatevoidgetInfo(){

//获得XML

URLurl;

try{

Stringfeed=getString(R.string.feed);

url=newURL(feed);

URLConnectionconnection=url.openConnection();

HttpURLConnectionhttpConnection=(HttpURLConnection)connection;

intresponseCode=httpConnection.getResponseCode();

if(responseCode==HttpURLConnection.HTTP_OK){

InputStreamin=httpConnection.getInputStream();

DocumentBuilderFactorydbfactory=DocumentBuilderFactory.newInstance();

DocumentBuilderdb=dbfactory.newDocumentBuilder();

//解析地震feed

Documentdom=db.parse(in);

ElementdocEle=dom.getDocumentElement();

//清空旧的地震信息

infoList.clear();

//获得地震信息的列表

NodeListnl=docEle.getElementsByTagName("entry");

if(nl!=null&&nl.getLength()>0){

for(inti=0;i<nl.getLength();i++){

Elemententry=(Element)nl.item(i);

Elementtitle=(Element)entry.getElementsByTagName("title").item(0);

Elementgeo=(Element)entry.getElementsByTagName("georss:point").item(0);

Elementwhen=(Element)entry.getElementsByTagName("updated").item(0);

Stringdetails=title.getFirstChild().getNodeValue();

Stringpoint=geo.getFirstChild().getNodeValue();

Stringdate=when.getFirstChild().getNodeValue();

SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");

Dateqdate=newGregorianCalendar(0,0,0).getTime();

try{

qdate=sdf.parse(date);

}catch(ParseExceptione){

e.printStackTrace();

}

String[]location=point.split("");

Locationloc=newLocation("dummyGPS");

loc.setLatitude(Double.parseDouble(location[0]));

loc.setLongitude(Double.parseDouble(location[1]));

StringmagnitudeString=details.split("")[1];

intend=magnitudeString.length()-1;

doublemagnitude=Double.parseDouble(magnitudeString.substring(0,end));

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

EarthQuakeInfoinfo=newEarthQuakeInfo(qdate,details,loc,magnitude);

//处理新的地震信息

newEntry(info);

}

}

}

}catch(Exceptione){

e.printStackTrace();

}

}

//新的地震信息

privatevoidnewEntry(EarthQuakeInfoinfo){

//将新的地址信息条目加入列表中

infoList.add(info);

//通知arrayadapter

adapter.notifyDataSetChanged();

}

//地震信息类

publicclassEarthQuakeInfo{

publicDatedate;

publicStringdetails;

publicLocationlocation;

publicdoublemagnitude;

publicEarthQuakeInfo(Dated,Stringde,Locationloc,doublemag){

date=d;

details=de;

location=loc;

magnitude=mag;

}

@Override

publicStringtoString(){

SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-dd''hh:mm:ss");

returnsdf.format(date)

+"\n里氏"

+magnitude

+"级\n"

+details;

}

}

}

相关推荐