android Pull解析复杂XML 转
16lz
2021-01-24
解析代码
Java代码- /**
- *从网络地址URL读取XML文件用Pull解析XML---解析jinghua2.xml
- *@paramxmlUrlPathxml的网络地址
- *@return阿福
- *@throwsException
- */
- publicstaticList<News>URLReadXmlByPull(StringxmlUrlPath)throwsException{
- List<News>listNews=newArrayList<News>();;
- Newsnews=null;
- URLurl=newURL(xmlUrlPath);
- //构建XmlPullParserFactory
- XmlPullParserFactorypullParserFactory=XmlPullParserFactory.newInstance();
- //获取XmlPullParser的实例
- XmlPullParserxmlPullParser=pullParserFactory.newPullParser();
- Log.i("PullParseXML","getXML......");
- //设置输入流xml文件装载器
- xmlPullParser.setInput(url.openConnection().getInputStream(),"UTF-8");
- //开始
- Log.i("PullParseXML","PullParseXML....start....");
- /**
- *pull读到xml后返回数字
- *读取到xml的声明返回数字0START_DOCUMENT;
- 读取到xml的结束返回数字1END_DOCUMENT;
- 读取到xml的开始标签返回数字2START_TAG
- 读取到xml的结束标签返回数字3END_TAG
- 读取到xml的文本返回数字4TEXT
- */
- inteventType=xmlPullParser.getEventType();
- /**
- *只要这个事件返回的不是1我们就一直读取xml文件
- */
- while(eventType!=XmlPullParser.END_DOCUMENT){
- StringnodeName=xmlPullParser.getName();
- switch(eventType){
- caseXmlPullParser.START_DOCUMENT:
- break;
- caseXmlPullParser.START_TAG:
- if("item".equals(nodeName)){
- news=newNews();
- }
- if("title".equals(nodeName)&&news!=null){
- news.setTitle(xmlPullParser.nextText());
- }
- if("link".equals(nodeName)&&news!=null){
- news.setLink(xmlPullParser.nextText());
- }
- if("author".equals(nodeName)&&news!=null){
- news.setAuthor(xmlPullParser.nextText());
- }
- if("guid".equals(nodeName)&&news!=null){
- news.setGuid(xmlPullParser.nextText());
- }
- if("image".equals(nodeName)&&news!=null){
- news.setImage(xmlPullParser.nextText());
- }
- if("video".equals(nodeName)&&news!=null){
- news.setVideo(xmlPullParser.nextText());
- }
- if("category".equals(nodeName)&&news!=null){
- news.setCategory(xmlPullParser.nextText());
- }
- if("pubDate".equals(nodeName)&&news!=null){
- news.setPubDate(xmlPullParser.nextText());
- }
- if("comments".equals(nodeName)&&news!=null){
- news.setComments(xmlPullParser.nextText());
- }
- if("description".equals(nodeName)&&news!=null){
- news.setDescription(xmlPullParser.nextText());
- }
- break;
- caseXmlPullParser.END_TAG:
- if("item".equals(nodeName)){
- listNews.add(news);
- }
- break;
- default:
- break;
- }
- eventType=xmlPullParser.next();
- }
- returnlistNews;
- }
使用时
Java代码- //从网络读取XML文件用Pull解析XML---解析jinghua2.xml(http://www.jinghua.cn/iphone/xml/bj.xml)
- publicvoidtestPullRead3()throwsThrowable{
- List<News>persons=ReadXmlByPullService.URLReadXmlByPull("http://www.jinghua.cn/iphone/xml/bj.xml");
- for(Newsperson:persons){
- Log.i(TAG,person.toString());
- }
- }
实体
Java代码- packagecom.yangguangfu.xml.domain;
- /**
- *新闻
- *
- *@author阿福
- *
- */
- publicclassNews{
- privateStringtitle;
- privateStringlink;
- privateStringauthor;
- privateStringguid;
- privateStringimage;
- privateStringvideo;
- privateStringcategory;
- privateStringpubDate;
- privateStringcomments;
- privateStringdescription;
- publicStringgetTitle(){
- returntitle;
- }
- publicvoidsetTitle(Stringtitle){
- this.title=title;
- }
- publicStringgetLink(){
- returnlink;
- }
- publicvoidsetLink(Stringlink){
- this.link=link;
- }
- publicStringgetAuthor(){
- returnauthor;
- }
- publicvoidsetAuthor(Stringauthor){
- this.author=author;
- }
- publicStringgetGuid(){
- returnguid;
- }
- publicvoidsetGuid(Stringguid){
- this.guid=guid;
- }
- publicStringgetImage(){
- returnimage;
- }
- publicvoidsetImage(Stringimage){
- this.image=image;
- }
- publicStringgetVideo(){
- returnvideo;
- }
- publicvoidsetVideo(Stringvideo){
- this.video=video;
- }
- publicStringgetCategory(){
- returncategory;
- }
- publicvoidsetCategory(Stringcategory){
- this.category=category;
- }
- publicStringgetPubDate(){
- returnpubDate;
- }
- publicvoidsetPubDate(StringpubDate){
- this.pubDate=pubDate;
- }
- publicStringgetComments(){
- returncomments;
- }
- publicvoidsetComments(Stringcomments){
- this.comments=comments;
- }
- publicStringgetDescription(){
- returndescription;
- }
- publicvoidsetDescription(Stringdescription){
- this.description=description;
- }
- @Override
- publicStringtoString(){
- return"title="+title+"guid="+guid+",link="+link
- +",description="+description+",image="+image+",video="
- +video+",category="+category+",author="+author
- +",pubDate="+pubDate+",comments="+comments;
- }
- }
更多相关文章
- android apk编译
- android 编写命令行测试程序
- android wifi连接
- Android仿苹果关机界面实现代码
- 使用MediaRecorder录制音频
- android去除标题栏-------全屏运行AND 无标题栏 总结
- android:文件下载
- Android(安卓)pulltorefresh上拉下拉刷新加载
- Android(安卓)使用 AIDL 实现进程间通信,使用基本类型作为输入和