XmlPullParser是android自带的xml解析器
public static ArrayList<TvUrl> getChannelsData(InputStream is) { ArrayList<TvUrl> list = null; TvUrl tvurl = null; XmlPullParserFactory pullFactory; try { pullFactory = XmlPullParserFactory.newInstance(); XmlPullParser xmlPullParser = pullFactory.newPullParser(); if(is == null) { return null; } xmlPullParser.setInput(is,"UTF-8"); int eventType = xmlPullParser.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { String tagName = xmlPullParser.getName(); switch (eventType) { case XmlPullParser.START_DOCUMENT: list = new ArrayList<TvUrl>(); break; case XmlPullParser.START_TAG: if("tv".equals(tagName)) { tvurl = new TvUrl(); } else if ("channelid".equals(tagName)) { tvurl.setid(xmlPullParser.nextText()); // Log.i(tvurl.getid()); } else if("name".equals(tagName)) { tvurl.setName(xmlPullParser.nextText()); // Log.i(tvurl.getName()); } else if("url".equals(tagName)) { String str = xmlPullParser.nextText(); tvurl.AddUrl(str); // Log.i(str); } break; case XmlPullParser.END_TAG: if ("tv".equals(tagName) && tvurl != null) { list.add(tvurl); } break; default: break; } eventType = xmlPullParser.next(); } } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; }
xmlPullParser.nextText() 是解析xml某个标签的值。
xmlPullParser.getAttributeValue(0) 是解析xml某个标签下中属性值,0代表位置为0的属性
原文链接:https://www.f2er.com/xml/299085.html