1、根据xml的结点定义JavaBean
<newslist> <news> <title>黑马52期就业快报</title> <detail>热烈祝贺黑马52期平均薪水突破13k</detail> <comment>15687</comment> <image>http://192.168.1.100:8080/images/6.jpg</image> </news> <news> <title>程序员因写代码太乱被杀害</title> <detail>凶手是死者同事,维护死者代码时完全看不懂而痛下杀手</detail> <comment>16359</comment> <image>http://192.168.1.100:8080/images/7.jpg</image> </news> </newslist>
public class News { private String title; private String detail; private String comment; private String imageUrl; @Override public String toString() { return "News [title=" + title + ",detail=" + detail + ",comment=" + comment + ",imageUrl=" + imageUrl + "]"; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDetail() { return detail; } public void setDetail(String detail) { this.detail = detail; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public String getImageUrl() { return imageUrl; } public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; } }
2 解析代码
public class MainActivity extends Activity { List<News> newsList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getNewsInfo(); } //开辟子线程下载新闻 private void getNewsInfo() { Thread t = new Thread(){ @Override public void run() { String path = "http://192.168.13.13:8080/news.xml"; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); //发送http GET请求,获取相应码 if(conn.getResponseCode() == 200){ InputStream is = conn.getInputStream(); //使用pull解析器,解析这个流 parseNewsXml(is); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; t.start(); } private void parseNewsXml(InputStream is) { XmlPullParser xp = Xml.newPullParser(); try { xp.setInput(is,"utf-8"); //对节点的事件类型进行判断,就可以知道当前节点是什么节点 int type = xp.getEventType(); News news = null; while(type != XmlPullParser.END_DOCUMENT){ switch (type) { case XmlPullParser.START_TAG: if("newslist".equals(xp.getName())){ newsList = new ArrayList<News>(); } else if("news".equals(xp.getName())){ news = new News(); } else if("title".equals(xp.getName())){ String title = xp.nextText(); news.setTitle(title); } else if("detail".equals(xp.getName())){ String detail = xp.nextText(); news.setDetail(detail); } else if("comment".equals(xp.getName())){ String comment = xp.nextText(); news.setComment(comment); } else if("image".equals(xp.getName())){ String image = xp.nextText(); news.setImageUrl(image); } break; case XmlPullParser.END_TAG: if("news".equals(xp.getName())){ //把news对象放着到集合中去 newsList.add(news); } break; } //解析完当前节点后,把指针移动至下一个节点,并返回它的事件类型 type = xp.next(); } //发消息,让主线程设置listview的适配器,如果消息不需要携带数据,可以发送空消息 handler.sendEmptyMessage(1); //用于测试 查看数据是否获取正确 // for (News n : newsList) { // System.out.println(n.toString()); // } } catch (Exception e) { e.printStackTrace(); } } }