码迷,mamicode.com
首页 > 其他好文 > 详细

Pull解析XML

时间:2017-04-24 18:56:48      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:protect   class   rri   nts   row   case   color   client   ring   

一.xml数据

<apps>
<app>
<id>1</id>
<name>Google Maps</name>
<version>1.0</version>
</app>
<app>
<id>2</id>
<name>Chrome</name>
<version>2.1</version>
</app>
<app>
<id>3</id>
<name>Google Play</name>
<version>2.3</version>
</app>
</apps>

 

二.关键方法

public class MainActivity extends Activity {

    TextView content;

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

        content = (TextView)findViewById(R.id.content);

    }

    protected void myClick(View v){
        if(v.getId() == R.id.btn){
            getXml();
        }
    }


    private void getXml(){

        OkHttpClient http = new OkHttpClient();
        final Request request = new Request.Builder()
                .url("http://192.168.2.100:8001/a.xml")
                .build();

        Call call = http.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                parseXmlPull(response.body().string());
            }
        });
    }

    private void parseXmlPull(String xmldata){
        try {
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = factory.newPullParser();
            parser.setInput(new StringReader(xmldata));
            int eventType = parser.getEventType();

            String id = "";
            String name = "";
            String version = "";

            while (eventType != XmlPullParser.END_DOCUMENT){
                String nodeName = parser.getName();
                switch (eventType){
                    // 开始解析节点
                    case XmlPullParser.START_TAG:
                        if( "id".equals(nodeName) ){
                            id = parser.nextText();
                        }else if("name".equals(nodeName)){
                            name = parser.nextText();
                        }
                        else if("version".equals(nodeName)){
                            version = parser.nextText();
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if("app".equals(nodeName)){
                            Log.e("id", id);
                            Log.e("name", name);
                            Log.e("version", version);
                        }
                        break;
                    default:
                        break;
                }
                eventType = parser.next();
            }


        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

 

Pull解析XML

标签:protect   class   rri   nts   row   case   color   client   ring   

原文地址:http://www.cnblogs.com/itfenqing/p/6758212.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!