温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
        在上文中介绍了使用sax方式解析xml,这里介绍下在Android中极力推荐的xmlpull方式解析xml。xmlpull不仅仅可以使用在Android上,同样也适用于javase,但在javase环境下,你需要自己去获取xmlpull所依赖的类库。
        环境:        

    kxml2-2.3.0.jar

    xmlpull_1_1_3_4c.jar

以上两个jar分别从以下网站下载:

http://www.xmlpull.org/    xmlpull

http://kxml.sourceforge.net/    kxml

xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    
<student id="1" group="1">
        
<name>张三</name>
        
<sex></sex>
        
<age>18</age>
        
<email>zhangsan@163.com</email>
        
<birthday>1987-06-08</birthday>
        
<memo>好学生</memo>
    
</student>
    
<student id="2" group="2">
        
<name>李四</name>
        
<sex></sex>
        
<age>18</age>
        
<email>lisi@163.com</email>
        
<birthday>1987-06-08</birthday>
        
<memo>好学生</memo>
    
</student>
    
<student id="3" group="3">
        
<name>小王</name>
        
<sex></sex>
        
<age>18</age>
        
<email>xiaowang@163.com</email>
        
<birthday>1987-06-08</birthday>
        
<memo>好学生</memo>
    
</student>
    
<student id="4" group="4">
        
<name>小张</name>
        
<sex></sex>
        
<age>18</age>
        
<email>xiaozhang@163.com</email>
        
<birthday>1987-06-08</birthday>
        
<memo>好学生</memo>
    
</student>
    
<student id="5" group="5">
        
<name>小明</name>
        
<sex></sex>
        
<age>18</age>
        
<email>xiaoming@163.com</email>
        
<birthday>1987-06-08</birthday>
        
<memo>好学生</memo>
    
</student>
</root>

Xml对应的实体bean文件:

package com.parsexml.entity;

public class Student {
    
    
private int id;
    
private int group;
    
private String name;
    
private String sex;
    
private int age;
    
private String email;
    
private String memo;
    
private String birthday;
    
public int getId() {
        
return id;
    }
    
public void setId(int id) {
        
this.id = id;
    }
    
public int getGroup() {
        
return group;
    }
    
public void setGroup(int group) {
        
this.group = group;
    }
    
public String getName() {
        
return name;
    }
    
public void setName(String name) {
        
this.name = name;
    }
    
public String getSex() {
        
return sex;
    }
    
public void setSex(String sex) {
        
this.sex = sex;
    }
    
public int getAge() {
        
return age;
    }
    
public void setAge(int age) {
        
this.age = age;
    }
    
public String getEmail() {
        
return email;
    }
    
public void setEmail(String email) {
        
this.email = email;
    }
    
public String getMemo() {
        
return memo;
    }
    
public void setMemo(String memo) {
        
this.memo = memo;
    }
    
public String getBirthday() {
        
return birthday;
    }
    
public void setBirthday(String birthday) {
        
this.birthday = birthday;
    }
    
}

Pull解析代码

package com.parsexml;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import com.parsexml.entity.Student;

public class PullParseXml {
    
    
    
public List<Student> PullParseXML(){
        
        List
<Student> list=null;
        Student student 
= null;
        
        
//构建XmlPullParserFactory
        try {
            XmlPullParserFactory pullParserFactory
=XmlPullParserFactory.newInstance();
            
//获取XmlPullParser的实例
            XmlPullParser xmlPullParser=pullParserFactory.newPullParser();
            
//设置输入流  xml文件
            xmlPullParser.setInput(Thread.currentThread().getContextClassLoader().getResourceAsStream("Student.xml"), "UTF-8");
            
            
//开始
            int eventType=xmlPullParser.getEventType();
            
            
try {
                
while(eventType!=XmlPullParser.END_DOCUMENT){
                    String nodeName
=xmlPullParser.getName();
                    
switch (eventType) {
                    
//文档开始
                    case XmlPullParser.START_DOCUMENT:
                        list
=new ArrayList<Student>();
                        
break;
                    
//开始节点
                    case XmlPullParser.START_TAG:
                        
//判断如果其实节点为student
                        if("student".equals(nodeName)){
                            
//实例化student对象
                            student=new Student();
                            
//设置Id属性
                            student.setId(Integer.parseInt(xmlPullParser.getAttributeValue(0)));
                            
//设置Group属性
                            student.setGroup(Integer.parseInt(xmlPullParser.getAttributeValue(1)));
                        }
else if("name".equals(nodeName)){
                            
//设置name
                            student.setName(xmlPullParser.nextText());
                        }
else if("sex".equals(nodeName)){
                            
//设置sex
                            student.setSex(xmlPullParser.nextText());
                        }
else if("age".equals(nodeName)){
                            
//设置age
                            student.setAge(Integer.parseInt(xmlPullParser.nextText()));
                        }
else if("email".equals(nodeName)){
                            
//设置email
                            student.setEmail(xmlPullParser.nextText());
                        }
else if("birthday".equals(nodeName)){
                            
//设置birthday
                            student.setBirthday(xmlPullParser.nextText());
                        }
else if("memo".equals(nodeName)){
                            
//设置memo属性
                            student.setMemo(xmlPullParser.nextText());
                        }
                        
break;
                    
//结束节点
                    case XmlPullParser.END_TAG:
                        
if("student".equals(nodeName)){
                            list.add(student);
                            student
=null;
                        }
                        
break;
                    
default:
                        
break;
                    }
                    eventType
=xmlPullParser.next();
                }
            } 
catch (NumberFormatException e) {
                e.printStackTrace();
            } 
catch (IOException e) {
                e.printStackTrace();
            }
        } 
catch (XmlPullParserException e) {
            e.printStackTrace();
        }

        
return list;
    }
    
    
public static void main(String[] args) {
        List
<Student> list=new PullParseXml().PullParseXML();
        
for(Student student:list){
            System.out.println(
"id:"+student.getId()+"\tgroup:"+student.getGroup()+"\tname:"+student.getName()+"\tsex:"+student.getSex()+"\tage:"+student.getAge()+"\temail:"+student.getEmail()+"\tbirthday:"+student.getBirthday()+"\tmemo:"+student.getMemo());
        }
    }
    
}
 

注:若工程中缺少kxml2-2.3.0.jar文件则会报以下异常信息:



 

你应该添加kxml2-2.3.0.jar包到你的classpath 

效果图:

点我下载demo


posted on 2010-08-04 08:12 雪山飞鹄 阅读(23897) 评论(6)  编辑  收藏 所属分类: javase

Feedback

# re: 轻松使用xmlpull解析xml 2010-08-04 11:07 java小爬虫
你好,请问一下,你的代码区的宽度很宽,这个是怎么设置的啊?我的很窄的。谢谢  回复  更多评论
  

# re: 轻松使用xmlpull解析xml 2010-08-04 11:15 雪山飞鹄
@java小爬虫
够认真的么,我也不晓得,我粘上去就是这样的,有可能跟blog模板的风格有关系吧  回复  更多评论
  

# re: 轻松使用xmlpull解析xml 2010-08-17 17:31 游客
说实在的,这个API看着够难用的  回复  更多评论
  

# re: 轻松使用xmlpull解析xml 2010-09-20 21:22 逍遥仙风
查了这么多资料,中文的都是瞎转载
英文的又看不明白,博主的文章真是受用呀ToT  回复  更多评论
  

# re: 轻松使用xmlpull解析xml[未登录] 2014-06-10 10:01 jack
很好,谢谢了!  回复  更多评论
  

# re: 轻松使用xmlpull解析xml 2016-05-05 17:58 发斯蒂芬
而发生的发生地方都是   回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: