Android基礎--XML解析(XmlPullParser )

來源:互聯網
上載者:User

標籤:android   style   blog   io   ar   color   os   sp   java   

1.要解析的xml檔案如下
<?xml version=‘1.0‘ encoding=‘utf-8‘ standalone=‘yes‘ ?><weather>    <city>        <name>深圳</name>        <temp>18°</temp>        <pm25>30</pm25>    </city>    <city>        <name>上海</name>        <temp>13°</temp>        <pm25>25</pm25>    </city>    <city>        <name>北京</name>        <temp>6°</temp>        <pm25>800</pm25>    </city></weather>
2.每個City節點對應的JavaBean如下:
public class City {    private String name;    private String temp;    private String pm25;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getTemp() {        return temp;    }    public void setTemp(String temp) {        this.temp = temp;    }    public String getPm25() {        return pm25;    }    public void setPm25(String pm25) {        this.pm25 = pm25;    }    public City(String name, String temp, String pm25) {        super();        this.name = name;        this.temp = temp;        this.pm25 = pm25;    }    public City() {        super();    }    @Override    public String toString() {        return "City [name=" + name + ", temp=" + temp + ", pm25=" + pm25 + "]";    }        }
3.進行解析的MainActivity.java如下:
public class MainActivity extends Activity {    List<City> cityList;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    public void click(View v){        //解析xml檔案        //1. 拿到資源檔        InputStream is = getClassLoader().getResourceAsStream("weather.xml");                //2. 拿到解析器對象        XmlPullParser xp = Xml.newPullParser();        try {            //3. 初始化xp對象            xp.setInput(is, "gbk");//根據檔案儲存的編碼決定                        //4.開始解析            //擷取當前節點的事件類型            int type = xp.getEventType();            City city = null;            while(type != XmlPullParser.END_DOCUMENT){                //判斷當前解析到哪一個節點,從而確定你要做什麼操作                switch (type) {                case XmlPullParser.START_TAG:                    //                    擷取當前節點的名字                    if("weather".equals(xp.getName())){                        cityList = new ArrayList<City>();                    }                    else if("city".equals(xp.getName())){                        city = new City();                    }                    else if("name".equals(xp.getName())){                        //                擷取當前節點的下一個節點的文本,把指標移動到當前節點的結束節點                        String name = xp.nextText();                        city.setName(name);                    }                    else if("temp".equals(xp.getName())){                        //                擷取當前節點的下一個節點的文本,把指標移動到當前節點的結束節點                        String temp = xp.nextText();                        city.setTemp(temp);                    }                    else if("pm25".equals(xp.getName())){                        //                擷取當前節點的下一個節點的文本,把指標移動到當前節點的結束節點                        String pm25 = xp.nextText();                        city.setPm25(pm25);                    }                    break;                case XmlPullParser.END_TAG:                    if("city".equals(xp.getName())){                        cityList.add(city);                    }                    break;                }                //把指標移動到下一個節點                type = xp.next();            }                        for (City c : cityList) {                System.out.println(c.toString());            }        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    }

 

Android基礎--XML解析(XmlPullParser )

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.