android中xml的pull解析

來源:互聯網
上載者:User

標籤:android中xml的pull解析

xml的pull解析:


   //類載入器載入xml檔案

InputStream is = MainActivity.class.getClassLoader().getResourceAsStream("weather.xml");

    //產生xml的pull解析器

    XmlPullParser pull = Xml.newPullParser();

    try {

    //設定輸入資料流

pull.setInput(is, "utf-8");

        //解析器當前處於的狀態

int type = pull.getEventType();

Weather weather = null ;

while (type != XmlPullParser.END_DOCUMENT) {

switch (type) {

            //當前是開始標籤的節點

    case XmlPullParser.START_TAG:

                //拿到節點標籤的名字

        String name = pull.getName();

            //判斷節點標籤的名字 (下面同這兒),然後放到weather對象中  

        if("channel".equals(name)){

            weather = new Weather();

            weather.setId(pull.getAttributeValue(0));

        }

            

        if("city".equals(name)){

            weather.setName(pull.nextText());

        }

        if("temp".equals(name)){

            weather.setTemp(pull.nextText());

        }

        if("wind".equals(name)){

            weather.setWind(pull.nextText());

        }

        break;

        //如果是結束標籤的節點,此處將weather對象放入list集合儲存

case XmlPullParser.END_TAG:

    String tagname = pull.getName();

    if("channel".equals(tagname)){

        list.add(weather);

    }

default:

    break;

        }

//此處必須寫,否則不會向下執行

        type = pull.next();

        }

    } catch (Exception e) {

e.printStackTrace();

    }

}



本文出自 “初學者” 部落格,請務必保留此出處http://10154241.blog.51cto.com/10144241/1664205

android中xml的pull解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.