解析代码

Java代码
  1. /**
  2. *从网络地址URL读取XML文件用Pull解析XML---解析jinghua2.xml
  3. *@paramxmlUrlPathxml的网络地址
  4. *@return阿福
  5. *@throwsException
  6. */
  7. publicstaticList<News>URLReadXmlByPull(StringxmlUrlPath)throwsException{
  8. List<News>listNews=newArrayList<News>();;
  9. Newsnews=null;
  10. URLurl=newURL(xmlUrlPath);
  11. //构建XmlPullParserFactory
  12. XmlPullParserFactorypullParserFactory=XmlPullParserFactory.newInstance();
  13. //获取XmlPullParser的实例
  14. XmlPullParserxmlPullParser=pullParserFactory.newPullParser();
  15. Log.i("PullParseXML","getXML......");
  16. //设置输入流xml文件装载器
  17. xmlPullParser.setInput(url.openConnection().getInputStream(),"UTF-8");
  18. //开始
  19. Log.i("PullParseXML","PullParseXML....start....");
  20. /**
  21. *pull读到xml后返回数字
  22. *读取到xml的声明返回数字0START_DOCUMENT;
  23. 读取到xml的结束返回数字1END_DOCUMENT;
  24. 读取到xml的开始标签返回数字2START_TAG
  25. 读取到xml的结束标签返回数字3END_TAG
  26. 读取到xml的文本返回数字4TEXT
  27. */
  28. inteventType=xmlPullParser.getEventType();
  29. /**
  30. *只要这个事件返回的不是1我们就一直读取xml文件
  31. */
  32. while(eventType!=XmlPullParser.END_DOCUMENT){
  33. StringnodeName=xmlPullParser.getName();
  34. switch(eventType){
  35. caseXmlPullParser.START_DOCUMENT:
  36. break;
  37. caseXmlPullParser.START_TAG:
  38. if("item".equals(nodeName)){
  39. news=newNews();
  40. }
  41. if("title".equals(nodeName)&&news!=null){
  42. news.setTitle(xmlPullParser.nextText());
  43. }
  44. if("link".equals(nodeName)&&news!=null){
  45. news.setLink(xmlPullParser.nextText());
  46. }
  47. if("author".equals(nodeName)&&news!=null){
  48. news.setAuthor(xmlPullParser.nextText());
  49. }
  50. if("guid".equals(nodeName)&&news!=null){
  51. news.setGuid(xmlPullParser.nextText());
  52. }
  53. if("image".equals(nodeName)&&news!=null){
  54. news.setImage(xmlPullParser.nextText());
  55. }
  56. if("video".equals(nodeName)&&news!=null){
  57. news.setVideo(xmlPullParser.nextText());
  58. }
  59. if("category".equals(nodeName)&&news!=null){
  60. news.setCategory(xmlPullParser.nextText());
  61. }
  62. if("pubDate".equals(nodeName)&&news!=null){
  63. news.setPubDate(xmlPullParser.nextText());
  64. }
  65. if("comments".equals(nodeName)&&news!=null){
  66. news.setComments(xmlPullParser.nextText());
  67. }
  68. if("description".equals(nodeName)&&news!=null){
  69. news.setDescription(xmlPullParser.nextText());
  70. }
  71. break;
  72. caseXmlPullParser.END_TAG:
  73. if("item".equals(nodeName)){
  74. listNews.add(news);
  75. }
  76. break;
  77. default:
  78. break;
  79. }
  80. eventType=xmlPullParser.next();
  81. }
  82. returnlistNews;
  83. }

使用时

Java代码
  1. //从网络读取XML文件用Pull解析XML---解析jinghua2.xml(http://www.jinghua.cn/iphone/xml/bj.xml)
  2. publicvoidtestPullRead3()throwsThrowable{
  3. List<News>persons=ReadXmlByPullService.URLReadXmlByPull("http://www.jinghua.cn/iphone/xml/bj.xml");
  4. for(Newsperson:persons){
  5. Log.i(TAG,person.toString());
  6. }
  7. }

实体

Java代码
  1. packagecom.yangguangfu.xml.domain;
  2. /**
  3. *新闻
  4. *
  5. *@author阿福
  6. *
  7. */
  8. publicclassNews{
  9. privateStringtitle;
  10. privateStringlink;
  11. privateStringauthor;
  12. privateStringguid;
  13. privateStringimage;
  14. privateStringvideo;
  15. privateStringcategory;
  16. privateStringpubDate;
  17. privateStringcomments;
  18. privateStringdescription;
  19. publicStringgetTitle(){
  20. returntitle;
  21. }
  22. publicvoidsetTitle(Stringtitle){
  23. this.title=title;
  24. }
  25. publicStringgetLink(){
  26. returnlink;
  27. }
  28. publicvoidsetLink(Stringlink){
  29. this.link=link;
  30. }
  31. publicStringgetAuthor(){
  32. returnauthor;
  33. }
  34. publicvoidsetAuthor(Stringauthor){
  35. this.author=author;
  36. }
  37. publicStringgetGuid(){
  38. returnguid;
  39. }
  40. publicvoidsetGuid(Stringguid){
  41. this.guid=guid;
  42. }
  43. publicStringgetImage(){
  44. returnimage;
  45. }
  46. publicvoidsetImage(Stringimage){
  47. this.image=image;
  48. }
  49. publicStringgetVideo(){
  50. returnvideo;
  51. }
  52. publicvoidsetVideo(Stringvideo){
  53. this.video=video;
  54. }
  55. publicStringgetCategory(){
  56. returncategory;
  57. }
  58. publicvoidsetCategory(Stringcategory){
  59. this.category=category;
  60. }
  61. publicStringgetPubDate(){
  62. returnpubDate;
  63. }
  64. publicvoidsetPubDate(StringpubDate){
  65. this.pubDate=pubDate;
  66. }
  67. publicStringgetComments(){
  68. returncomments;
  69. }
  70. publicvoidsetComments(Stringcomments){
  71. this.comments=comments;
  72. }
  73. publicStringgetDescription(){
  74. returndescription;
  75. }
  76. publicvoidsetDescription(Stringdescription){
  77. this.description=description;
  78. }
  79. @Override
  80. publicStringtoString(){
  81. return"title="+title+"guid="+guid+",link="+link
  82. +",description="+description+",image="+image+",video="
  83. +video+",category="+category+",author="+author
  84. +",pubDate="+pubDate+",comments="+comments;
  85. }
  86. }

更多相关文章

  1. android apk编译
  2. android 编写命令行测试程序
  3. android wifi连接
  4. Android仿苹果关机界面实现代码
  5. 使用MediaRecorder录制音频
  6. android去除标题栏-------全屏运行AND 无标题栏 总结
  7. android:文件下载
  8. Android(安卓)pulltorefresh上拉下拉刷新加载
  9. Android(安卓)使用 AIDL 实现进程间通信,使用基本类型作为输入和

随机推荐

  1. c语言实现删除字符串中的数字字符
  2. c语言如何判断整数是几位数
  3. getchar()的功能是什么
  4. c语言数组的定义及赋值
  5. c语言怎么用scanf输入字符串
  6. 如何用c语言输出100到200之间的素数
  7. vb和c语言有什么区别
  8. c++中string的用法介绍
  9. xcode怎么用
  10. c编译程序的功能是什么