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();
            }
        }

  • 相关阅读:
    设计模式六大原则
    .net Stream篇(七)
    .net Stream篇(六)
    .net Stream篇(五)
    .net Stream篇(四)
    Leetcode 18
    Leetcode 16
    Leetcode 15
    Leetcode 12
    Leetcode 9
  • 原文地址:https://www.cnblogs.com/fanzhiguo/p/5789728.html
Copyright © 2011-2022 走看看