布局文件:
<RelativeLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/btn_pull" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="读取"/> <Button android:id="@+id/btn_cpull" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/btn_pull" android:layout_alignParentRight="true" android:layout_below="@+id/btn_pull" android:layout_marginTop="28dp" android:text="生成" /> </RelativeLayout>
定义一个Person实体类:
private Integer id; private String name; private Short age;
public class PullXmlService { public Person currentPerson; // 声明返回值 public List<Person> readXML(InputStream is) { List<Person> persons = null; // 首先利用XML.newPullParser()获取解析对象 XmlPullParser xmlPullParser = Xml.newPullParser(); try {// 解析文件 xmlPullParser.setInput(is,"UTF-8"); // 获取解析的事件类型 int eventType = xmlPullParser.getEventType(); // 判断文件解析的是否完毕 while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { case XmlPullParser.START_DOCUMENT: persons = new ArrayList<Person>(); break; case XmlPullParser.START_TAG: String tagName = xmlPullParser.getName(); Log.v("PullXmlService",tagName + "------"); // 当解析的标签是person时 if ("person".equals(tagName)) { // 创建person对象 currentPerson = new Person(); // 设置id属性 需要进行转化 currentPerson.setId(Integer.parseInt(xmlPullParser.getAttributeValue(null,"id"))); } else if ("name".equals(tagName)) { currentPerson.setName(xmlPullParser.nextText()); } else if ("age".equals(tagName)) { currentPerson.setAge(new Short(xmlPullParser.nextText())); } break; case XmlPullParser.END_TAG: if ("person".equals(xmlPullParser.getName()) && currentPerson != null) { // 把person对象添加到集合中 persons.add(currentPerson); currentPerson = null; } break; } // 下一个标签标识 eventType = xmlPullParser.next(); } // 关闭流 is.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // 返回 return persons; } /** * 采用pull生成XML文件 * * @param persons * @return */ public boolean write(List<Person> persons) { // 采用pull解析进行实现 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File sdCardDir = Environment.getExternalStorageDirectory(); File file = new File(sdCardDir,"mycsdn.xml"); Log.e("保存的路径是",sdCardDir+"/mysdk"); XmlSerializer serializer = Xml.newSerializer(); FileOutputStream fos = null; try { // 根据文件对象创建一个文件的输出流对象 fos = new FileOutputStream(file); // 设置输出的流及编码 serializer.setOutput(fos,"UTF-8"); // 设置文件的开始 serializer.startDocument("UTF-8",true); // persons标签开始 serializer.startTag(null,"persons"); for (Person person : persons) { serializer.startTag(null,"person"); serializer.attribute("null","id",person.getId() + ""); // 设置person标签的子标签 name serializer.startTag(null,"name"); serializer.text(person.getName()); serializer.endTag(null,"name"); // 设置person标签的子标签的age serializer.startTag(null,"age"); serializer.text(person.getAge() + ""); serializer.endTag(null,"age"); // person标签的结束 serializer.endTag(null,"person"); } serializer.endTag(null,"persons"); serializer.endDocument(); fos.flush(); fos.close(); return true; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } return false; } }
主类:
public class MainActivity extends Activity { // pull解析器业务对象 public PullXmlService pullXmlService; public Button btn_pull,btn_cpull; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置显示的视图 setContentView(R.layout.activity_main); pullXmlService = new PullXmlService(); // 首先获取按钮组件对象 btn_pull = (Button) findViewById(R.id.btn_pull); btn_cpull = (Button) findViewById(R.id.btn_cpull); // 注册事件 btn_pull.setOnClickListener(new MyOnClickListener()); btn_cpull.setOnClickListener(new MyOnClickListener()); } class MyOnClickListener implements View.OnClickListener { @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.btn_pull: List<Person> persons = null; try { persons = pullXmlService.readXML(getAssets().open("csdn.xml")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (persons != null) { Toast.makeText(MainActivity.this,"采用pull解析xml文件" + persons.get(0).getName() + "--" + persons.get(1).getName(),Toast.LENGTH_LONG).show(); } break; case R.id.btn_cpull: try { List<Person> persons1 = pullXmlService.readXML(getAssets().open("csdn.xml")); // 重新生成一个xml文件 mycsdn.xml boolean flag = pullXmlService.write(persons1); if (flag) { Toast.makeText(MainActivity.this,"采用pull解析生产xml文件成功",Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this,"采用pull解析生产xml文件失败",Toast.LENGTH_LONG).show(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; default: break; } } } }
运行的结果如下(mnt/shell/emulated/0目录下有这个文件):