首页 > 代码库 > 网络获取数据的Xml的Pull解析

网络获取数据的Xml的Pull解析

网络获取的XML的Pull解析

  <?xml version="1.0" encoding="utf-8" ?>
- <students>
 
- <student xuehao="1233">
  <name>张三</name>
  <address>北京市昌平区</address>
- <call>
  <phone>010-37464567</phone>
  <tel>1384950448</tel>
  </call>
  <school>北京大学</school>
  </student>

- <student xuehao="1235">
  <name>张三</name>
  <address>北京海淀</address>
- <call>
  <phone>010-37545457</phone>
  <tel>13849599978</tel>
  </call>
  <school>清华大学</school>
  </student>


  </students>

//----------------------以上为要解析的XML---------------------------------------------

//-------------------------------这里text是网络获取得到的数据----------Students  是一个集合--------bean包里的数据没有写---------------

/*
     * <student xuehao="1233">里面的xuehao是由Parser.getAttributeValue(0)得到的
     * <name>张三</name> 里面的name是由Parser.nextText()得到的
     */

 

//解析XML
    private void toXNL(String text) {
        try {
            Students studentss=null;
            //将读取到的结果码   添加到XML里面
            XmlPullParser Parser = Xml.newPullParser();
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(text.getBytes());
            Parser.setInput(byteArrayInputStream, "utf-8");
            //通过parse获得项目类型      相当于文件里面的属性吧
            int eventType = Parser.getEventType();
            //判断
            while ((eventType != XmlPullParser.END_DOCUMENT)) {
                String name = Parser.getName();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (name.equals("student")) {
                       studentss=new Students();
                       studentss.setXuehao(Parser.getAttributeValue(0));
                    }
                    else if (name.equals("name")) {
                        String nname = Parser.nextText();
                       studentss.setName(nname);
                    } else if (name.equals("address")) {
                        String address = Parser.nextText();
                        studentss.setAddress(address);
                    } else if (name.equals("phone")) {
                        String phone = Parser.nextText();
                        studentss.setPhone(phone);
                    } else if (name.equals("tel")) {
                        String tel = Parser.nextText();
                        studentss.setTel(tel);
                    }else if (name.equals("school")) {
                        String school = Parser.nextText();


                        studentss.setSchool(school);
                    }
                    
                    break;
                    case XmlPullParser.END_TAG:
                        if (name.equals("student")) {

//添加到集合
                            students.add(studentss);
                        }
                        break;
                default:
                    break;
                }
                eventType=Parser.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    };

网络获取数据的Xml的Pull解析