pull解析是安卓自带的解析方式
具体使用直接看代码
1 private List<NewsBody> xmlParser(InputStream inputStream) { 2 try { 3 XmlPullParserFactory factory= XmlPullParserFactory.newInstance(); 4 //获取XmlPullParser实例 5 XmlPullParser pullParser=factory.newPullParser(); 6 pullParser.setInput(inputStream, "UTF-8"); 7 //开始 8 int eventType=pullParser.getEventType(); 9 10 while(eventType!=XmlPullParser.END_DOCUMENT){ 11 String nodeName=pullParser.getName(); 12 switch (eventType) { 13 //文档开始 14 case XmlPullParser.START_DOCUMENT: 15 bodies=new ArrayList<NewsBody>(); 16 break; 17 //开始节点 18 case XmlPullParser.START_TAG: 19 if("item".equals(nodeName)){ 20 body=new NewsBody(); 21 }else if("title".equals(nodeName)){ 22 body.setTitle(pullParser.nextText()); 23 }else if("copyright".equals(nodeName)){ 24 body.setCopyright(pullParser.nextText()); 25 }else if("pubDate".equals(nodeName)){ 26 body.setPubDate(pullParser.nextText()); 27 }else if("link".equals(nodeName)){ 28 body.setlink(pullParser.nextText()); 29 } 30 break; 31 //结束节点 32 case XmlPullParser.END_TAG: 33 if("item".equals(nodeName)){ 34 bodies.add(body); 35 // body=null; 36 } 37 break; 38 default: 39 break; 40 } 41 // 手动的触发下一个事件 42 eventType=pullParser.next(); 43 } 44 } catch (Exception e) { 45 // TODO Auto-generated catch block 46 e.printStackTrace(); 47 } 48 return bodies; 49 }