Java 之解析XML


<? xml  version = "1.0"  encoding = "UTF-8" ?>
<s tundets >
     < stundet   id = "23" >
         < name >李明</ name >
         < age >30</ age >
     </ person >
     < studnet  id = "20" >
         < name >李向梅</ name >
         < age >25</ age >
     </ stundet >
</ studets >


    List<Student> studentList=new ArrayList<>();
    private ListView lv_main_list;
    private MyAdater myAdater;
    private ProgressDialog progressDialog;




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


        //实例化进度条对话框
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在加载中.....");




        lv_main_list = (ListView) findViewById(R.id.lv_main_list);


        //实例化适配器
        myAdater = new MyAdater();
        //设置适配器
        lv_main_list.setAdapter(myAdater);


    }
    class MyAdater extends BaseAdapter{


        @Override
        public int getCount() {
            return studentList.size();
        }


        @Override
        public Object getItem(int i) {
            return studentList.get(i);
        }


        @Override
        public long getItemId(int i) {
            return i;
        }


        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {


            LinearLayout layout=new LinearLayout(MainActivity.this);
            layout.setOrientation(LinearLayout.HORIZONTAL);


            TextView textViewName=new TextView(MainActivity.this);
            textViewName.setText(studentList.get(i).getName());
            TextView textViewsex=new TextView(MainActivity.this);
            textViewsex.setText(studentList.get(i).getSex());


            layout.addView(textViewName);
            layout.addView(textViewsex);


            return layout;
        }
    }








    public void getXML(View view){
        new MyTask().execute();
    }


    class MyTask extends AsyncTask{


        private Student student;


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog.show();
        }


        @Override
        protected Object doInBackground(Object[] objects) {
            //获取网络数据XML


            //01.确定网络路径
            String path="http://193.168.4.164:8080/G150825_S2SH/studentActiongetXML.action";
            try {
                //02.实例化URL
                URL url=new URL(path);
                //03.获取连接对象
                HttpURLConnection connection= (HttpURLConnection) url.openConnection();
                //04.设置请求方式
                connection.setRequestMethod("GET");
                //05.设置请求连接超时的时间(优化)
                connection.setConnectTimeout(5000);


                //06.获取响应码,结果码
                int code=connection.getResponseCode();
                if(code==200){
                    //07.获取服务器返回过来的数据
                    InputStream is=connection.getInputStream();
                    //测试(打印)
                    //缓冲字符流
//                    BufferedReader br=new BufferedReader(new InputStreamReader(is));
//                    String str=null;
//                    while((str=br.readLine())!=null){
//                        Log.i("test",str);
//                    }


                    //解析PULL SAX  基于事件驱动
                    XmlPullParser xpp=Xml.newPullParser();
                    try {
                        xpp.setInput(is,"UTF-8");


                        int type=xpp.getEventType();


                        while(type!=XmlPullParser.END_DOCUMENT){
                            switch (type) {


                                case XmlPullParser.START_TAG:
                                    //获取开始标签的名字
                                    String startTagName=xpp.getName();
                                    if("student".equals(startTagName)){
                                        student = new Student();
                                        //获取name属性值
                                        String name=xpp.getAttributeValue(0);
                                        student.setName(name);
                                    }else if("sex".equals(startTagName)){
                                        //获取sex的文本值
                                        String sex=xpp.nextText();
                                        student.setSex(sex);
                                    }
                                    break;
                                case XmlPullParser.END_TAG:
                                    //获取到结束标签的名字
                                    String endTagName=xpp.getName();
                                    if("student".equals(endTagName)){
                                        studentList.add(student);
                                    }
                                    break;
                            }
                            type=xpp.next();
                        }
                    } catch (XmlPullParserException e) {
                        e.printStackTrace();
                    }
                }


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }


        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);


            //通知适配器发生改变
            myAdater.notifyDataSetChanged();


            progressDialog.cancel();
        }
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值