zoukankan      html  css  js  c++  java
  • pull方法解析xml

    private void getData() {
            new Thread() {

                public void run() {
                    try {
                        URL url = new URL(pathUrl);
                        HttpURLConnection httpURLConnection = (HttpURLConnection) url
                                .openConnection();
                        httpURLConnection.setReadTimeout(5000);
                        httpURLConnection.setConnectTimeout(5000);
                        httpURLConnection.setRequestMethod("GET");
                        httpURLConnection.connect();
                        int code = httpURLConnection.getResponseCode();
                        if (code == 200) {
                            InputStream inputStream = httpURLConnection
                                    .getInputStream();
                             BufferedReader bufferedReader=new BufferedReader(new
                             InputStreamReader(inputStream,"utf-8"));
                             String line;
                             StringBuffer stringBuffer=new StringBuffer();
                             while((line=bufferedReader.readLine())!=null){
                             stringBuffer.append(line);
                             }
                             String ss=stringBuffer.toString();
                             Message message=new Message();
                             message.obj=ss;
                             handler.sendMessage(message);

                        }
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                };
            }.start();
        }

    private void readXml(String ss) {
            // TODO Auto-generated method stub
            List<Bean> list=null;
            Bean bean =null;
            try {
                xmlPullParserFactory = XmlPullParserFactory.newInstance();
                XmlPullParser pullParser = xmlPullParserFactory.newPullParser();
                ByteArrayInputStream inputStream = new ByteArrayInputStream(ss.getBytes());
                pullParser.setInput(inputStream, "utf-8");
                int type = pullParser.getEventType();
                while (type != XmlPullParser.END_DOCUMENT) {
                    String name = pullParser.getName();
                    switch (type) {
                    case XmlPullParser.START_DOCUMENT:
                            list=new ArrayList<Bean>();
                        break;
                    case XmlPullParser.START_TAG:
                        if("blog".equals(name)){
                            bean=new Bean();
                            Log.i("-------", "bean");
                        }else
                        if("id".equals(name)){
                            String str = pullParser.nextText();
                            bean.setId(str);
                            
                        }else if("title".equals(name)){
                            String na = pullParser.nextText();
                            bean.setTitle(na);
                        }
                        
                        break;
                    case XmlPullParser.END_TAG:
                        if("blog".equals(name)){
                            list.add(bean);
                            //bean=null;
                        }
                        break;
                    }
                    type=pullParser.next();
                }
                for (Bean bean2 : list) {
                    text.append(bean2.getId()+bean2.getTitle());
                }
            } catch (XmlPullParserException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

  • 相关阅读:
    个人永久性免费-Excel催化剂功能第103波-批量打开多文件或多链接
    个人永久性免费-Excel催化剂插件功能修复与更新汇总篇之八
    个人永久性免费-Excel催化剂功能第101波-批量替换功能(增加正则及高性能替换能力)
    个人永久性免费-Excel催化剂功能第99波-手机号码归属地批量查询
    个人永久性免费-Excel催化剂功能第100波-透视多行数据为多列数据结构
    个人永久性免费-Excel催化剂功能第98波-零代码零距离轻松接触并拥有金融大数据
    个人永久性免费-Excel催化剂功能第97波-快递单号批量查询物流信息
    个人永久性免费-Excel催化剂功能第95波-地图数据挖宝之IP地址转地理地址及不同经纬度版本转换
    个人永久性免费-Excel催化剂功能第96波-地图数据挖宝之全国天气查询(区域最细可到区县,最长预报4天)
    实现兼容document.querySelector的方法
  • 原文地址:https://www.cnblogs.com/fanzhiguo/p/5789728.html
Copyright © 2011-2022 走看看