这一章我们来学习Android中的XML解析器,Android中自带了三个XML解析器,有PULL,SAX,DOM解析器,其中PULL跟SAX都是以事件作为驱动导向的解析器,优点是占用内存小,处理速度快,DOM是将整个XML放入内存中再解析,处理速度要稍差一些,但DOM也有自己的优点,可以在解析的时候适当增加节点.OK,我们先来看一下我们要解析的xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <persons>
- <person id = "1">
- <status>大徙弟</status>
- <name>孙悟空</name>
- <tool>金箍棒</tool>
- <number>杀死了50只妖怪</number>
- </person>
-
- <person id = "2">
- <status>二徙弟</status>
- <name>猪八戒</name>
- <tool>九齿钉耙 </tool>
- <number>杀死了40只妖怪</number>
- </person>
-
- <person id = "3">
- <status>三徙弟</status>
- <name>沙和尚</name>
- <tool>降妖宝杖</tool>
- <number>杀死了30只妖怪</number>
- </person>
-
- </persons>
这里面定义了一些个人的信息和其他具体信息,好了,存入模拟器的SD目录下,接下来,我们先定义JavaBean,Person.java
- public class Person
- {
- private String id;
- private String status;
- private String name;
- private String tool;
- private String number;
-
-
- public String getId()
- {
- return id;
- }
- public void setId(String id)
- {
- this.id = id;
- }
- public String getStatus()
- {
- return status;
- }
- public void setStatus(String status)
- {
- this.status = status;
- }
- public String getName()
- {
- return name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public String getTool()
- {
- return tool;
- }
- public void setTool(String tool)
- {
- this.tool = tool;
- }
- public String getNumber()
- {
- return number;
- }
- public void setNumber(String number)
- {
- this.number = number;
- }
- @Override
- public String toString()
- {
- return "Person [id=" + id + ", status=" + status + ", name=" + name
- + ", tool=" + tool + ", number=" + number + "]";
- }
-
- }
对应上面xml的文件中的数据,接下来就是重点喽,要好好学哦,呵呵,PULL解析类PullHandler.java
OK,解析类写完了,测试一下吧,用上一节课的单元测试试一下呢,对于每个小方法做单元测试很重要,这要能及时解决出现的问题,呵呵,我快像唐僧一样了,阿弥陀佛,要是不明日章元测试的话,就看一下
Android[中级教程]第四章 单元测试AndroidTestCase
- import java.io.File;
- import java.io.FileInputStream;
- import java.util.List;
-
- import android.os.Environment;
- import android.test.AndroidTestCase;
-
- public class PullHandlerTest extends AndroidTestCase
- {
- public void testGetPersons(){
-
-
- File SD_Files = Environment.getExternalStorageDirectory();
-
- String file_path = SD_Files.getName() + File.separator + "persons.xml";
- try
- {
- FileInputStream fis = new FileInputStream(new File(file_path));
- PullHandler pullHandler = new PullHandler(fis);
- List<Person> persons = pullHandler.getPersons();
-
- System.out.println(persons);
-
- } catch (Exception e)
- {
-
- e.printStackTrace();
- }
-
-
- }
- }
测试类写完了,测试一下吧,OK,接下来就是定义main.xml了
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView android:textAppearance="?android:attr/textAppearanceMedium"
- android:layout_width="wrap_content" android:text="唐僧的三个徙弟"
- android:layout_height="wrap_content" android:id="@+id/textView1"
- android:paddingLeft="10dip" android:paddingBottom="10dip"></TextView>
- <ListView android:id="@+id/xml_list" android:layout_height="wrap_content"
- android:layout_width="match_parent"></ListView>
-
- </LinearLayout>
定义了一个ListView和一个TextView,ListView是准备显示xml中的数据的
再定义一个list_item.xml是每一行要显示数据的样式
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout android:layout_height="wrap_content"
- android:id="@+id/linearLayout1" android:layout_width="match_parent">
- <TextView android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:layout_width="wrap_content" android:text="ID: "
- android:paddingLeft="10dp"></TextView>
- <TextView android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:layout_width="wrap_content" android:text="TextView"
- android:id="@+id/id"></TextView>
- <TextView android:layout_height="wrap_content"
- android:textAppearance="?android:attr/textAppearanceMedium"
- android:layout_width="wrap_content" android:text="TextView"
- android:id="@+id/name" android:paddingLeft="30dp"></TextView>
- </LinearLayout>
- <LinearLayout android:layout_height="wrap_content"
- android:id="@+id/linearLayout2" android:layout_width="match_parent">
- <TextView android:layout_height="wrap_content"
- android:layout_width="wrap_content" android:text="杀死了"
- android:paddingLeft="10dp"></TextView>
- <TextView android:text="TextView" android:id="@+id/number"
- android:layout_width="wrap_content" android:layout_height="wrap_content"
- android:textColor="#FF0000"></TextView>
- <TextView android:text="只妖怪" android:layout_width="wrap_content"
- android:layout_height="wrap_content"></TextView>
- </LinearLayout>
-
- </LinearLayout>
好了,都定义完了,最后就是我们的Activity了
- </pre><pre class="java" name="code">import java.io.File;
- import java.io.FileInputStream;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Environment;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
-
- public class PullActivity extends Activity
- {
-
- private ListView listView;
- private SimpleAdapter adapter;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
-
- super.onCreate(savedInstanceState);
- setContentView(R.layout.xml_handler);
-
- listView = (ListView)findViewById(R.id.xml_list);
- try
- {
-
- getAdapter();
-
- } catch (Exception e)
- {
- e.printStackTrace();
- }
- listView.setAdapter(adapter);
- }
-
-
- private void getAdapter() throws Exception
- {
- List<Map<String, String>> lists = new ArrayList<Map<String, String>>();
-
-
- File SD_Files = Environment.getExternalStorageDirectory();
- String file_path = SD_Files.getName() + File.separator + "persons.xml";
- FileInputStream input = new FileInputStream(new File(file_path));
- PullHandler pullHandler = new PullHandler(input);
- List<Person> persons = pullHandler.getPersons();
-
-
- Map<String, String> map;
- for(Person p : persons){
- map = new HashMap<String, String>();
-
- map.put("id", p.getId());
- map.put("status", p.getStatus());
- map.put("name", p.getName());
- map.put("tool", p.getTool());
- map.put("number", p.getNumber());
-
- lists.add(map);
- }
-
-
- String[] from = {"id", "status", "name", "tool", "number"};
-
- int[] to = {R.id.item_id, R.id.item_status, R.id.item_name, R.id.item_tool, R.id.item_number};
-
- adapter = new SimpleAdapter(this, lists, R.layout.handler_list_item, from, to);
-
- }
- }
好了,最后就是上图了
这一章有点长,希望同学们都看完,因为接下来的SAX解析和DOM解析,我们还需要用到这里面的模板,谢谢