Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android中通過pull解釋器解析xml

android中通過pull解釋器解析xml

編輯:關於Android編程

android中已經集成了pull解釋器,所以不用添加jar文件,如果在javaee中使用pull需要手動添加pull的jar文件。android系統本身就是通過pull解釋器來處理xml文件。所以說明pull還是有一定的優越性。
1、main.xml
[html] 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
 
    <ListView 
        android:id="@+id/lv" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        /> 
 
</LinearLayout> 

2、item.xml
[html] 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent"  www.2cto.com
    android:layout_height="wrap_content" /> 

3、我將person.xml文件放到了android的assets文件夾下
[html]
<?xml version="1.0" encoding="UTF-8"?> 
<persons> 
    <person id="23"> 
        <name>jack</name> 
        <age>16</age> 
    </person> 
    <person id="24"> 
        <name>tom</name> 
        <age>15</age> 
    </person> 
</persons> 
4、MainActivity.java
[java] 
public class MainActivity extends Activity { 
 
    private PersonService ps = null; 
     
    private ListView lv; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        ps = new PersonService(); 
        lv = (ListView) this.findViewById(R.id.lv); 
        try { 
            AssetManager am = getAssets(); 
            InputStream is = am.open("person.xml"); 
            List<Person> list = ps.getPersons(is); 
            List<String> names = new ArrayList<String>(); 
            for(Person p:list){ 
                String name = p.getName(); 
                names.add(name); 
                System.out.println(name); 
            } 
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item, 0,names); 
            lv.setAdapter(adapter); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } 
         
    } 

5、PersonService.java
[java] 
public class PersonService { 
     
    public List<Person> getPersons(InputStream xml) throws Exception{ 
        XmlPullParser pullParser = Xml.newPullParser(); 
        List<Person> persons = null; 
        Person person = null; 
        pullParser.setInput(xml, "utf-8"); 
        int event = pullParser.getEventType(); 
         
        //判斷是否解釋到文檔結束部分 
        while (event != XmlPullParser.END_DOCUMENT) { 
            switch (event) { 
             
            case XmlPullParser.START_DOCUMENT: 
                persons = new ArrayList<Person>(); 
                break; 
            case XmlPullParser.START_TAG: 
                if("person".equals(pullParser.getName())){ 
                    //得到標簽中的第一個參數的值 
                    int id = Integer.parseInt(pullParser.getAttributeValue(0)); 
                    person = new Person(); 
                    person.setId(id); 
                }    
                if("name".equals(pullParser.getName())){ 
                    //得到標簽中間的文本內容 
                    String name = pullParser.nextText(); 
                    person.setName(name); 
                } 
                if("age".equals(pullParser.getName())){ 
                    int age = Integer.parseInt(pullParser.nextText()); 
                    person.setAge(age); 
                } 
                break; 
                 
            case XmlPullParser.END_TAG://如果解釋標簽是perosn就將person對象存放到list中 
                if("person".equals(pullParser.getName())){ 
                    persons.add(person); 
                } 
                break; 
                 
    case XmlPullParser.END_DOCUMENT: 
                 
                break; 
                 
            default: 
                break; 
            } 
            //pull不會主動向後執行,所以自己調用next方法並把值付給event 
            event = pullParser.next(); 
        } 
        System.out.println("persons.size()-->"+persons.size()); 
         
         
        return persons; 
    } 


6、Person.java
[java] 
public class Person { 
 
    private Integer id; 
    private String name; 
    private Integer age; 
     
     
    public Integer getId() { 
        return id; 
    } 
    public void setId(Integer id) { 
        this.id = id; 
    } 
    public String getName() { 
        return name; 
    } 
    public void setName(String name) { 
        this.name = name; 
    } 
    public Integer getAge() { 
        return age; 
    } 
    public void setAge(Integer age) { 
        this.age = age; 
    } 
     
     

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved