用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

Anroid Pull解析

2013-11-02 作者: chow举报

[android]代码库

<?xml version="1.0" encoding="utf-8"?>  
<students>  
    <student id="20110806100">  
        <name>小明</name>  
        <age>22</age>  
        <sex>男</sex>  
    </student>  
    <student id="20110806101">  
        <name>小李</name>  
        <age>24</age>  
        <sex>男</sex>  
    </student>  
    <student id="20110806102">  
        <name>小丽</name>  
        <age>21</age>  
        <sex>女</sex>  
    </student>  
</students>  
/////////////////////////////////////////////////////


package com.andyidea.entity;  
  
public class Student {  
      
    private String id;  
    private String name;  
    private int age;  
    private String sex;  
      
    public String getId() {  
        return id;  
    }  
    public void setId(String id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public int getAge() {  
        return age;  
    }  
    public void setAge(int age) {  
        this.age = age;  
    }  
    public String getSex() {  
        return sex;  
    }  
    public void setSex(String sex) {  
        this.sex = sex;  
    }  
  
}  


///////////////////////////////////////

package com.andyidea.util;  
  
import java.io.InputStream;  
import java.util.ArrayList;  
import java.util.List;  
  
import org.xmlpull.v1.XmlPullParser;  
import org.xmlpull.v1.XmlPullParserFactory;  
  
import android.util.Xml;  
  
import com.andyidea.entity.Student;  
  
public class ParserByPULL {  
      
    //采用XmlPullParser来解析XML文件    
    public static List<Student> getStudents(InputStream inStream) throws Throwable   
    {    
        List<Student> students = null;    
        Student mStudent = null;    
          
        //========创建XmlPullParser,有两种方式=======  
        //方式一:使用工厂类XmlPullParserFactory  
        XmlPullParserFactory pullFactory = XmlPullParserFactory.newInstance();  
        XmlPullParser parser = pullFactory.newPullParser();  
        //方式二:使用Android提供的实用工具类android.util.Xml  
        //XmlPullParser parser = Xml.newPullParser();    
          
        //解析文件输入流    
        parser.setInput(inStream, "UTF-8");    
        //产生第一个事件    
        int eventType = parser.getEventType();    
        //只要不是文档结束事件,就一直循环    
        while(eventType!=XmlPullParser.END_DOCUMENT)    
        {    
            switch (eventType)     
            {    
                //触发开始文档事件    
                case XmlPullParser.START_DOCUMENT:    
                    students = new ArrayList<Student>();    
                    break;    
                //触发开始元素事件    
                case XmlPullParser.START_TAG:    
                    //获取解析器当前指向的元素的名称    
                    String name = parser.getName();    
                    if("student".equals(name))    
                    {    
                        //通过解析器获取id的元素值,并设置student的id    
                        mStudent = new Student();    
                        mStudent.setId(parser.getAttributeValue(0));    
                    }    
                    if(mStudent!=null)    
                    {    
                        if("name".equals(name))    
                        {    
                            //获取解析器当前指向元素的下一个文本节点的值    
                            mStudent.setName(parser.nextText());    
                        }    
                        if("age".equals(name))    
                        {    
                            //获取解析器当前指向元素的下一个文本节点的值    
                            mStudent.setAge(new Short(parser.nextText()));    
                        }  
                        if("sex".equals(name))  
                        {  
                            //获取解析器当前指向元素的下一个文本节点的值    
                            mStudent.setSex(parser.nextText());  
                        }  
                    }    
                    break;    
                //触发结束元素事件    
                case XmlPullParser.END_TAG:    
                    //    
                    if("student".equals(parser.getName()))    
                    {    
                        students.add(mStudent);    
                        mStudent = null;    
                    }    
                    break;    
                default:    
                    break;    
            }    
            eventType = parser.next();    
        }    
        return students;    
    }    
  
}  
/////////////////////////

package com.andyidea.xmlparser;  
  
import java.io.InputStream;  
import java.util.List;  
  
import com.andyidea.entity.Student;  
import com.andyidea.util.ParserByPULL;  
  
import android.app.Activity;  
import android.content.res.AssetManager;  
import android.os.Bundle;  
import android.util.Log;  
  
public class MainActivity extends Activity {  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        AssetManager asset = getAssets();  
        try {  
            InputStream input = asset.open("student.xml");  
            List<Student> list = ParserByPULL.getStudents(input);  
            for (Student stu : list) {  
                   Log.e("StudentInfo","Person ID: " + stu.getId() + ","  
                         + stu.getName() + ", " + stu.getAge() + ", "  
                         + stu.getSex());  
            }  
        } catch (Throwable e) {  
            e.printStackTrace();  
        }  
    }  
}  


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...