PULL解析XML文件

来源:互联网 发布:银行业务流程优化 编辑:程序博客网 时间:2024/05/16 05:01
 //XML文件 </strong></span>-<oschina>   <catalog>0</catalog>   <newsCount>0</newsCount>   <pagesize>20</pagesize>  -<newslist>    -<news>     <id>74612</id>     <title> <![CDATA[Qampp 2.5.6 正式发布,集成 Subversion]]> </title>     <body><![CDATA[更新日志: 1: 新集成Subversion 1.9.4, 协作开发SVN工具. 2: Apache 的配...]]></body>     <commentCount>3</commentCount>     <author><![CDATA[Tuesday]]></author>     <authorid>998019</authorid>     <pubDate>2016-06-24 09:47:04</pubDate>     <url/>  -<newstype>     <type>0</type>     <authoruid2>998019</authoruid2>     <eventurl/>   </newstype>         </news>   </newslist></oschina>------------------------------------------------------------- public class MainActivity extends Activity {    private String path = "http://www.oschina.net/action/api/news_list";    List<Root> root_list;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);         // 开启子线程联网请求数据         new Thread() {            public void run() {                getdata();// 获取数据             }        }.start();    };              

//获取数据方法

private void getdata(){

try {

   URL url = new URL(path);//加载路径
            HttpURLConnection huc = (HttpURLConnection) url.openConnection();// 获得连接网络的UI对象
            huc.setConnectTimeout(5000);// 设置请求超时
            huc.setReadTimeout(5000);// 设置读超时
            huc.setRequestMethod("GET");//请求方式
            huc.connect();// 获得连接网络

            if (huc.getResponseCode() == 200) {
                System.out.println("连接成功");
                InputStream inputStream = huc.getInputStream();// 获得输入流
                XmlPullParser parser = Xml.newPullParser();
                parser.setInput(inputStream, "UTF-8");
                int eventType = parser.getEventType();

                Root root = null;//封装类
                List<Root> root_list = null;//数据存入的集合
                while (eventType != XmlPullParser.END_DOCUMENT) {
                    switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:// 文档开始事件,可以进行数据初始化处理
                        root_list = new ArrayList<Root>();//实例化集合
                        break;
                    case XmlPullParser.START_TAG:// 开始元素事件
                        String item = parser.getName();
                        if (item.equalsIgnoreCase("news")) {//news开始
                            root = new Root();//实例化类
                        } else if (root != null) {//判断类不为空

                            if (item.equals("id")) { //与id比较
                                root.setId(parser.nextText());// 如果后面是Text元素,即返回它的值
                            } else if (item.equalsIgnoreCase("title")) {
                                root.setTitle(parser.nextText());
                            } else if (item.equalsIgnoreCase("body")) {
                                root.setBody(parser.nextText());
                            } else if (item.equalsIgnoreCase("commentCount")) {
                                root.setCommentCount(parser.nextText());
                            } else if (item.equalsIgnoreCase("author")) {
                                root.setAuthor(parser.nextText());
                            } else if (item.equalsIgnoreCase("authorid")) {
                                root.setAuthorid(parser.nextText());
                            } else if (item.equalsIgnoreCase("pubDate")) {
                                root.setPubDate(parser.nextText());
                            } else if (item.equalsIgnoreCase("url")) {
                                root.setUrl(parser.nextText());
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:// 结束元素事件
                        if (parser.getName().equalsIgnoreCase("news")//news结束
                                && root != null) {//类不为空
                            root_list.add(root); //把数据存入集合
                            root = null; //设置类为空
                        }
                        break;
                    }
                    eventType = parser.next();
                }
   //测试遍历集合
                for (Root root2 : root_list) {
                    System.out.println(root2.toString());
                }



//handler发送到主线程
                Message msg = Message.obtain();
                msg.obj = root_list;
                msg.what = 1;
                handler.sendMessage(msg);



            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }// 获urlUI对象
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    };


    

    }
}


                                             
0 0
原创粉丝点击