android解析xml文件 Android DOM解析XML之全球实时地震信息列表

public class HttpGet extends Activity {

  private ListView list;

  EarthQuakeInfo selectedQuake;

  ArrayAdapter adapter;

  ArrayList infoList=new ArrayList();

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  this.setContentView(R.layout.main);

  //设置listView的内容为infoList

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

  adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,infoList);

  //设置ListView的适配器为adapter

  list.setAdapter(adapter);

  getInfo();//获得infoList的具体内容。

  }

  private void getInfo(){

  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 (Exception e) {

  e.printStackTrace();// TODO: handle exception

  }

  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) {

  // TODO: handle exception

  }

  }

  private void newEntry(EarthQuakeInfo info){

  infoList.add(info);

  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){

  this.date=d;

  this.details=de;

  this.location=loc;

  this.magnitude=mag;

  }

  @Override

  public String toString(){

  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd' 'hh:mm:ss");

  return sdf.format(date)+"\n里氏"+magnitude+"级"+details+"地点"+location;

  }

  }

  }

相关推荐