• Pull解析xml文件


    <?xml version="1.0" encoding="utf-8"?>
        <books>  
            <book>  
                <id>1001</id>  
                <name>Thinking In Java</name>  
                <price>80.00</price>  
            </book>  
            <book>  
                <id>1002</id>  
                <name>Core Java</name>  
                <price>90.00</price>  
            </book>  
            <book>  
                <id>1003</id>  
                <name>Hello, Andriod</name>  
                <price>100.00</price>  
            </book>  
        </books>  
    微 2016/3/30 19:45:14

    package test.bwie.com.lianxi0302;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.util.Xml;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;

    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserException;

    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;

    /**
     * Created by Administrator on 2016/3/3.
     */
    public class XmlJiexi extends Activity {

        List<Book> books = null;
        Book book = null;
        private ArrayAdapter adapter;
        private ListView listviwe;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.xmljie);

            listviwe = (ListView)findViewById(R.id.listview);

            XmlPullParser parser = Xml.newPullParser(); //由android.util.Xml创建一个XmlPullParser实例
            try {
                InputStream is = getAssets().open("books.xml");
                parser.setInput(is, "UTF-8"); //设置输入流 并指明编码方式
                int eventType = parser.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT) {
                    switch (eventType) {
                        case XmlPullParser.START_DOCUMENT:
                            books = new ArrayList<Book>();
                            break;
                        case XmlPullParser.START_TAG:
                            if (parser.getName().equals("book")) {
                                book = new Book();
                            } else if (parser.getName().equals("id")) {
                                eventType = parser.next();
                                book.setId(Integer.parseInt(parser.getText()));
                            } else if (parser.getName().equals("name")) {
                                eventType = parser.next();
                                book.setName(parser.getText());
                            } else if (parser.getName().equals("price")) {
                                eventType = parser.next();
                                book.setPrice(Float.parseFloat(parser.getText()));
                            }
                            break;
                        case XmlPullParser.END_TAG:
                            if (parser.getName().equals("book")) {
                                books.add(book);
                                Log.e("Tag","books====="+books);
                                book = null;
                            }
                            break;
                    }
                    eventType = parser.next();
                }
               
            } catch (IOException e1) {
                e1.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            }
            adapter = new ArrayAdapter(XmlJiexi.this,android.R.layout.simple_list_item_1,books);
            listviwe.setAdapter(adapter);
        }
    }
  • 相关阅读:
    LOL 计蒜客
    cf1486 D. Max Median
    P3567 [POI2014]KUR-Couriers
    dp 求物品组合情况
    黑暗爆炸
    hdu5306 Gorgeous Sequence
    P4609 [FJOI2016]建筑师
    cf 1342 E. Placing Rooks
    重修dp-背包
    acwing 2154. 梦幻布丁
  • 原文地址:https://www.cnblogs.com/weiyangge/p/5338678.html
Copyright © 2020-2023  润新知