android Pull解析复杂XML 转
解析代码
Java代码
- /**
- * 从网络地址URL读取XML文件用Pull解析XML---解析jinghua2.xml
- * @param xmlUrlPath xml的网络地址
- * @return 阿福
- * @throws Exception
- */
- public static List<News> URLReadXmlByPull(String xmlUrlPath) throws Exception{
- List<News> listNews = new ArrayList<News>();;
- News news = null;
- URL url = new URL(xmlUrlPath);
- //构建XmlPullParserFactory
- XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();
- //获取XmlPullParser的实例
- XmlPullParser xmlPullParser = pullParserFactory.newPullParser();
- Log.i("PullParseXML", "getXML......");
- //设置输入流 xml文件装载器
- xmlPullParser.setInput(url.openConnection().getInputStream(), "UTF-8");
- //开始
- Log.i("PullParseXML", "PullParseXML....start....");
- /**
- * pull读到xml后 返回数字
- * 读取到xml的声明返回数字0 START_DOCUMENT;
- 读取到xml的结束返回数字1 END_DOCUMENT ;
- 读取到xml的开始标签返回数字2 START_TAG
- 读取到xml的结束标签返回数字3 END_TAG
- 读取到xml的文本返回数字4 TEXT
- */
- int eventType=xmlPullParser.getEventType();
- /**
- * 只要这个事件返回的不是1 我们就一直读取xml文件
- */
- while(eventType != XmlPullParser.END_DOCUMENT){
- String nodeName=xmlPullParser.getName();
- switch (eventType) {
- case XmlPullParser.START_DOCUMENT:
- break;
- case XmlPullParser.START_TAG:
- if("item".equals(nodeName)){
- news = new News();
- }
- 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;
- case XmlPullParser.END_TAG:
- if("item".equals(nodeName)){
- listNews.add(news);
- }
- break;
- default:
- break;
- }
- eventType = xmlPullParser.next();
- }
- return listNews;
- }
使用时
Java代码
- //从网络读取XML文件用Pull解析XML---解析jinghua2.xml(http://www.jinghua.cn/iphone/xml/bj.xml)
- public void testPullRead3() throws Throwable{
- List<News> persons = ReadXmlByPullService.URLReadXmlByPull("http://www.jinghua.cn/iphone/xml/bj.xml");
- for(News person : persons){
- Log.i(TAG, person.toString());
- }
- }
实体
Java代码
- package com.yangguangfu.xml.domain;
- /**
- * 新闻
- *
- * @author 阿福
- *
- */
- public class News {
- private String title;
- private String link;
- private String author;
- private String guid;
- private String image;
- private String video;
- private String category;
- private String pubDate;
- private String comments;
- private String description;
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getLink() {
- return link;
- }
- public void setLink(String link) {
- this.link = link;
- }
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- public String getGuid() {
- return guid;
- }
- public void setGuid(String guid) {
- this.guid = guid;
- }
- public String getImage() {
- return image;
- }
- public void setImage(String image) {
- this.image = image;
- }
- public String getVideo() {
- return video;
- }
- public void setVideo(String video) {
- this.video = video;
- }
- public String getCategory() {
- return category;
- }
- public void setCategory(String category) {
- this.category = category;
- }
- public String getPubDate() {
- return pubDate;
- }
- public void setPubDate(String pubDate) {
- this.pubDate = pubDate;
- }
- public String getComments() {
- return comments;
- }
- public void setComments(String comments) {
- this.comments = comments;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- @Override
- public String toString() {
- return "title=" + title + "guid=" + guid + ",link=" + link
- + ",description=" + description + ",image=" + image + ",video="
- + video + ",category=" + category + ",author=" + author
- + ",pubDate=" + pubDate + ",comments=" + comments;
- }
- }