android开发之读取xml文件(2)

1.获取xml
2.构造DOM
3.获取xml内容
            /*获取xml*/
            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();
     
              /*构造DOM*/   
      DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbfactory.newDocumentBuilder();

// 解析地震feed
      Document dom = db.parse(in);     
      Element docEle = dom.getDocumentElement();
       
      // 清空旧的地震信息
      infoList.clear();
      
              /*获取xml内容*/  
      // 获得地震信息的列表
      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();
                } 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwygdg.html