linkai1

导航

* pull方法解析xml

  1.  private static List<Schoolnews> parseXML(InputStream in)  
  2.             throws XmlPullParserException, IOException {  
  3.   
  4.         List<Schoolnewssnews = null;  
  5.         Schoolnews schoolnews = null;  
  6.         XmlPullParser pullParser = Xml.newPullParser();  
  7.         pullParser.setInput(in, "UTF-8");  
  8.         int event = pullParser.getEventType();  
  9.         while (event != XmlPullParser.END_DOCUMENT) {  
  10.             switch (event) {  
  11.             case XmlPullParser.START_DOCUMENT:  
  12.                 snews = new ArrayList<Schoolnews>();  
  13.                 break;  
  14.             case XmlPullParser.START_TAG:  
  15.                 if ("Schoolnews".equals(pullParser.getName())) {  
  16.                     int id = new Integer(pullParser.getAttributeValue(0));  
  17.                     schoolnews = new Schoolnews();  
  18.                     schoolnews.setId(id);  
  19.                 }  
  20.                   
  21.                 if ("title".equals(pullParser.getName())) {  
  22.                     schoolnews.setTitle(pullParser.nextText());  
  23.                 }  
  24.                 if ("content".equals(pullParser.getName())) {  
  25.                     schoolnews.setContent(pullParser.nextText());  
  26.                 }  
  27.                 if ("sharedate".equals(pullParser.getName())) {  
  28.                     schoolnews.setShareDate(pullParser.nextText());  
  29.                 }  
  30.   
  31.                 if ("shareperson".equals(pullParser.getName())) {  
  32.                     schoolnews.setShareperson(pullParser.nextText());  
  33.                 }  
  34.                   
  35.                 if ("pictureuri".equals(pullParser.getName())) {  
  36.                     schoolnews.setPictureuri(pullParser.nextText());  
  37.                 }  
  38.   
  39.                 break;  
  40.             case XmlPullParser.END_TAG:  
  41.                 if ("Schoolnews".equals(pullParser.getName())) {  
  42.                     snews.add(schoolnews);  
  43.                     schoolnews = null;  
  44.                 }  
  45.                 break;  
  46.   
  47.             }  
  48.             event = pullParser.next();  
  49.         }  
  50.         return snews;  
  51.   
  52.     }  

posted on 2016-08-20 10:00  linkai1  阅读(67)  评论(0编辑  收藏  举报