环境:
kxml2-2.3.0.jar
xmlpull_1_1_3_4c.jar
以上两个jar分别从以下网站下载:
http://www.xmlpull.org/ xmlpull
http://kxml.sourceforge.net/ kxml
xml文件:
<?
xmlversion="1.0"encoding="UTF-8"
?>
< root >
student id ="1" group ="1" name >张三 </ sex >男 age >18 email >zhangsan@163.com birthday >1987-06-08 memo >好学生 student ="2" ="2" >李四 >女 >lisi@163.com ="3" ="3" >小王 >xiaowang@163.com ="4" ="4" >小张 >xiaozhang@163.com ="5" ="5" >小明 >xiaoming@163.com >
>
< root >
student id ="1" group ="1" name >张三 </ sex >男 age >18 email >zhangsan@163.com birthday >1987-06-08 memo >好学生 student ="2" ="2" >李四 >女 >lisi@163.com ="3" ="3" >小王 >xiaowang@163.com ="4" ="4" >小张 >xiaozhang@163.com ="5" ="5" >小明 >xiaoming@163.com >
>
Xml对应的实体bean文件:
packagecom.parsexml.entity;
public classStudent{
private intid;
intgroup;
privateStringname;
privateStringsex;
intage;
privateStringemail;
privateStringmemo;
privateStringbirthday;
intgetId(){
returnid;
}
voidsetId( intid){
this.id=id;
}
intgetGroup(){
returngroup;
}
voidsetGroup( intgroup){
this.group=group;
}
publicStringgetName(){
returnname;
}
voidsetName(Stringname){
this.name=name;
}
publicStringgetSex(){
returnsex;
}
voidsetSex(Stringsex){
this.sex=sex;
}
intgetAge(){
returnage;
}
voidsetAge( intage){
this.age=age;
}
publicStringgetEmail(){
returnemail;
}
voidsetEmail(Stringemail){
this.email=email;
}
publicStringgetMemo(){
returnmemo;
}
voidsetMemo(Stringmemo){
this.memo=memo;
}
publicStringgetBirthday(){
returnbirthday;
}
voidsetBirthday(Stringbirthday){
this.birthday=birthday;
}
}
public classStudent{
private intid;
intgroup;
privateStringname;
privateStringsex;
intage;
privateStringemail;
privateStringmemo;
privateStringbirthday;
intgetId(){
returnid;
}
voidsetId( intid){
this.id=id;
}
intgetGroup(){
returngroup;
}
voidsetGroup( intgroup){
this.group=group;
}
publicStringgetName(){
returnname;
}
voidsetName(Stringname){
this.name=name;
}
publicStringgetSex(){
returnsex;
}
voidsetSex(Stringsex){
this.sex=sex;
}
intgetAge(){
returnage;
}
voidsetAge( intage){
this.age=age;
}
publicStringgetEmail(){
returnemail;
}
voidsetEmail(Stringemail){
this.email=email;
}
publicStringgetMemo(){
returnmemo;
}
voidsetMemo(Stringmemo){
this.memo=memo;
}
publicStringgetBirthday(){
returnbirthday;
}
voidsetBirthday(Stringbirthday){
this.birthday=birthday;
}
}
Pull解析代码
packagecom.parsexml;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.List;
importorg.xmlpull.v1.XmlPullParser;
importorg.xmlpull.v1.XmlPullParserException;
importorg.xmlpull.v1.XmlPullParserFactory;
importcom.parsexml.entity.Student;
classPullParseXml{
publicList<Student>PullParseXML(){
List<Student>list= null;
Studentstudent= null;
// 构建XmlPullParserFactory
try{
XmlPullParserFactorypullParserFactory=XmlPullParserFactory.newInstance();
获取XmlPullParser的实例
XmlPullParserxmlPullParser=pullParserFactory.newPullParser();
设置输入流xml文件
xmlPullParser.setInput(Thread.currentThread().getContextClassLoader().getResourceAsStream("Student.xml"),"UTF-8");
开始 inteventType=xmlPullParser.getEventType();
try{
while(eventType!=XmlPullParser.END_DOCUMENT){
StringnodeName=xmlPullParser.getName();
switch(eventType){
文档开始 caseXmlPullParser.START_DOCUMENT:
list= newArrayList<Student>();
break;
开始节点 caseXmlPullParser.START_TAG:
判断如果其实节点为student if("student".equals(nodeName)){
实例化student对象
student= newStudent();
设置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());
} if("sex".equals(nodeName)){
设置sex
student.setSex(xmlPullParser.nextText());
} if("age".equals(nodeName)){
设置age
student.setAge(Integer.parseInt(xmlPullParser.nextText()));
} if("email".equals(nodeName)){
设置email
student.setEmail(xmlPullParser.nextText());
} if("birthday".equals(nodeName)){
设置birthday
student.setBirthday(xmlPullParser.nextText());
} if("memo".equals(nodeName)){
设置memo属性
student.setMemo(xmlPullParser.nextText());
}
结束节点 caseXmlPullParser.END_TAG:
if("student".equals(nodeName)){
list.add(student);
student= null;
}
default:
break;
}
eventType=xmlPullParser.next();
}
} catch(NumberFormatExceptione){
e.printStackTrace();
} catch(IOExceptione){
e.printStackTrace();
}
} catch(XmlPullParserExceptione){
e.printStackTrace();
}
returnlist;
}
static voidmain(String[]args){
List<Student>list= newPullParseXml().PullParseXML();
for(Studentstudent: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()); } } }
原文链接:https://www.f2er.com/xml/296859.htmlimportjava.io.IOException;
importjava.util.ArrayList;
importjava.util.List;
importorg.xmlpull.v1.XmlPullParser;
importorg.xmlpull.v1.XmlPullParserException;
importorg.xmlpull.v1.XmlPullParserFactory;
importcom.parsexml.entity.Student;
classPullParseXml{
publicList<Student>PullParseXML(){
List<Student>list= null;
Studentstudent= null;
// 构建XmlPullParserFactory
try{
XmlPullParserFactorypullParserFactory=XmlPullParserFactory.newInstance();
获取XmlPullParser的实例
XmlPullParserxmlPullParser=pullParserFactory.newPullParser();
设置输入流xml文件
xmlPullParser.setInput(Thread.currentThread().getContextClassLoader().getResourceAsStream("Student.xml"),"UTF-8");
开始 inteventType=xmlPullParser.getEventType();
try{
while(eventType!=XmlPullParser.END_DOCUMENT){
StringnodeName=xmlPullParser.getName();
switch(eventType){
文档开始 caseXmlPullParser.START_DOCUMENT:
list= newArrayList<Student>();
break;
开始节点 caseXmlPullParser.START_TAG:
判断如果其实节点为student if("student".equals(nodeName)){
实例化student对象
student= newStudent();
设置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());
} if("sex".equals(nodeName)){
设置sex
student.setSex(xmlPullParser.nextText());
} if("age".equals(nodeName)){
设置age
student.setAge(Integer.parseInt(xmlPullParser.nextText()));
} if("email".equals(nodeName)){
设置email
student.setEmail(xmlPullParser.nextText());
} if("birthday".equals(nodeName)){
设置birthday
student.setBirthday(xmlPullParser.nextText());
} if("memo".equals(nodeName)){
设置memo属性
student.setMemo(xmlPullParser.nextText());
}
结束节点 caseXmlPullParser.END_TAG:
if("student".equals(nodeName)){
list.add(student);
student= null;
}
default:
break;
}
eventType=xmlPullParser.next();
}
} catch(NumberFormatExceptione){
e.printStackTrace();
} catch(IOExceptione){
e.printStackTrace();
}
} catch(XmlPullParserExceptione){
e.printStackTrace();
}
returnlist;
}
static voidmain(String[]args){
List<Student>list= newPullParseXml().PullParseXML();
for(Studentstudent: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()); } } }