• XML解析
  1. 获取XML解析器 XmlPullParser parser=XML.newPullParser();
  2. 设置解析器要解析的内容parser.setInput(Inputstream in,"utf-8");
  3. 获取解析事件 parser.getEventType();
  4. 往下解析while(type!=XmlPullParser.END_DOCUMEN){}
public class XmlUtils {
    public static List<news> parserXml(InputStream inputStream) throws XmlPullParserException, IOException {
        List<news> list=null;
        news mynews=null;
//        1、创建解析器
         XmlPullParser xmlPullParser= Xml.newPullParser();
//        2、 设置要解析的内容
        xmlPullParser.setInput(inputStream,"UTF-8");
//        3、获取解析事件类型
        int eventType = xmlPullParser.getEventType();
//        4、开始解析
        while(eventType!=XmlPullParser.END_DOCUMENT){
            switch (eventType){
                case XmlPullParser.START_TAG://开始标签
                    if ("channel".equals(xmlPullParser.getName())){
                        list=new ArrayList<news>();
                    }else if ("item".equals(xmlPullParser.getName())){

                        mynews = new news();
                    }else if ("title".equals(xmlPullParser.getName())){
                        mynews.setTitle(xmlPullParser.nextText());
                    }else if ("link".equals(xmlPullParser.getName())){
                        mynews.setLink(xmlPullParser.nextText());
                    }else if ("description".equals(xmlPullParser.getName())){
                        mynews.setDescription(xmlPullParser.nextText());
                    }
                    break;
                case XmlPullParser.END_TAG://结束标签
                    if ("item".equals(xmlPullParser.getName())){
                        list.add(mynews);
                    }
                    break;
            }
        }
        return list;
    }
}
10-13 05:45