`
心雨心
  • 浏览: 351558 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

阅读更多
  1. public class HttpGet extends Activity {   
  2.   
  3.   private ListView list;   
  4.   
  5.   EarthQuakeInfo selectedQuake;   
  6.   
  7.   ArrayAdapter adapter;   
  8.   
  9.   ArrayList infoList=new ArrayList();   
  10.   
  11.   @Override  
  12.   
  13.   protected void onCreate(Bundle savedInstanceState) {   
  14.   
  15.   super.onCreate(savedInstanceState);   
  16.   
  17.   this.setContentView(R.layout.main);   
  18.   
  19.   //设置listView的内容为infoList   
  20.   
  21.   list=(ListView)this.findViewById(R.id.list);   
  22.   
  23.   adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,infoList);   
  24.   
  25.   //设置ListView的适配器为adapter   
  26.   
  27.   list.setAdapter(adapter);   
  28.   
  29.   getInfo();//获得infoList的具体内容。   
  30.   
  31.   }   
  32.   
  33.   private void getInfo(){   
  34.   
  35.   URL url;   
  36.   
  37.   try{   
  38.   
  39.   String feed=getString(R.string.feed);   
  40.   
  41.   url=new URL(feed);   
  42.   
  43.   URLConnection connection=url.openConnection();   
  44.   
  45.   HttpURLConnection httpConnection=(HttpURLConnection)connection;   
  46.   
  47.   int responseCode=httpConnection.getResponseCode();   
  48.   
  49.   if(responseCode==HttpURLConnection.HTTP_OK){   
  50.   
  51.   InputStream in=httpConnection.getInputStream();   
  52.   
  53.   DocumentBuilderFactory dbfactory=DocumentBuilderFactory.newInstance();   
  54.   
  55.   DocumentBuilder db=dbfactory.newDocumentBuilder();   
  56.   
  57.   //解析地震feed   
  58.   
  59.   Document dom=db.parse(in);   
  60.   
  61.   Element docEle=dom.getDocumentElement();   
  62.   
  63.   //清空旧的地震信息   
  64.   
  65.   infoList.clear();   
  66.   
  67.   //获得地震信息列表   
  68.   
  69.   NodeList nl=docEle.getElementsByTagName("entry");   
  70.   
  71.   if(nl!=null&&nl.getLength()>0){   
  72.   
  73.   for(int i=0;i<nl.getLength();i++){   
  74.   
  75.   Element entry=(Element)nl.item(i);   
  76.   
  77.   Element title=(Element)entry.getElementsByTagName("title").item(0);   
  78.   
  79.   Element geo=(Element)entry.getElementsByTagName("georss:point").item(0);   
  80.   
  81.   Element when=(Element)entry.getElementsByTagName("updated").item(0);   
  82.   
  83.     
  84.   
  85.   String details=title.getFirstChild().getNodeValue();   
  86.   
  87.   String point=geo.getFirstChild().getNodeValue();   
  88.   
  89.   String date=when.getFirstChild().getNodeValue();   
  90.   
  91.   SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");   
  92.   
  93.   Date qdate=new GregorianCalendar(0,0,0).getTime();   
  94.   
  95.   try{   
  96.   
  97.   qdate=sdf.parse(date);   
  98.   
  99.   }catch (Exception e) {   
  100.   
  101.   e.printStackTrace();// TODO: handle exception   
  102.   
  103.   }   
  104.   
  105.   String[] location=point.split(" ");   
  106.   
  107.   Location loc=new Location("dummyGPS");   
  108.   
  109.   loc.setLatitude(Double.parseDouble(location[0]));   
  110.   
  111.   loc.setLongitude(Double.parseDouble(location[1]));   
  112.   
  113.   String magnitudeString=details.split(" ")[1];   
  114.   
  115.   int end=magnitudeString.length()-1;   
  116.   
  117.   double magnitude=Double.parseDouble(magnitudeString.substring(0,end));   
  118.   
  119.   details=details.split(",")[1].trim();   
  120.   
  121.   EarthQuakeInfo info=new EarthQuakeInfo(qdate,details,loc,magnitude);   
  122.   
  123.   //处理地震信息   
  124.   
  125.   newEntry(info);   
  126.   
  127.   }   
  128.   
  129.   }   
  130.   
  131.   }   
  132.   
  133.   }catch (Exception e) {   
  134.   
  135.   // TODO: handle exception   
  136.   
  137.   }   
  138.   
  139.   }   
  140.   
  141.   private void newEntry(EarthQuakeInfo info){   
  142.   
  143.   infoList.add(info);   
  144.   
  145.   adapter.notifyDataSetChanged();   
  146.   
  147.   }   
  148.   
  149.   public class EarthQuakeInfo{   
  150.   
  151.   public Date date;   
  152.   
  153.   public String details;   
  154.   
  155.   public Location location;   
  156.   
  157.   public double magnitude;   
  158.   
  159.   public EarthQuakeInfo(Date d,String de,Location loc,double mag){   
  160.   
  161.   this.date=d;   
  162.   
  163.   this.details=de;   
  164.   
  165.   this.location=loc;   
  166.   
  167.   this.magnitude=mag;   
  168.   
  169.   }   
  170.   
  171.   @Override  
  172.   
  173.   public String toString(){   
  174.   
  175.   SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd' 'hh:mm:ss");   
  176.   
  177.   return sdf.format(date)+"\n里氏"+magnitude+"级"+details+"地点"+location;   
  178.   
  179.   }   
  180.   
  181.   }   
  182.   
  183.   }  
分享到:
评论
1 楼 zhao1111 2012-09-12  
请问楼主哪里提供地震的xml文件啊

相关推荐

Global site tag (gtag.js) - Google Analytics