android中通過pull解譯器解析xml

來源:互聯網
上載者:User

         android中已經整合了pull解譯器,所以不用添加jar檔案,如果在javaee中使用pull需要手動添加pull的jar檔案。android系統本身就是通過pull解譯器來處理xml檔案。所以說明pull還是有一定的優越性。
        1、main.xml
        <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
      <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content" />

    3、我將person.xml檔案放到了android的assets檔案夾下
    <?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
     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
     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
    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;
   }


   }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.