详细代码看附件!

布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:padding="@dimen/padding_medium"        android:text="@string/hello_world"        tools:context=".MainActivity" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/textView1"        android:layout_centerHorizontal="true"        android:text="@string/button1" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/button1"        android:layout_below="@+id/button1"        android:text="@string/button2" /></RelativeLayout>


适配文件:
dimens.xml:<resources>    <dimen name="padding_small">8dp</dimen>    <dimen name="padding_medium">8dp</dimen>    <dimen name="padding_large">16dp</dimen></resources>strings.xml:<resources>    <string name="app_name">ReadWriteXML</string>    <string name="button1">读取</string>    <string name="button2">写入</string>    <string name="hello_world">Hello world!</string>    <string name="menu_settings">Settings</string>    <string name="title_activity_main">xml读写</string></resources>


Person类:
package com.app.bean;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;}@Overridepublic String toString() {return "Person [id=" + id + ", name=" + name + ", age=" + age + "]";}public Person(Integer id, String name, Integer age) {this.id = id;this.name = name;this.age = age;}public Person() {}}


MainActivity:
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.Toast;import com.app.bean.Person;import com.app.readwritexmliii.R;import com.app.service.PersonService;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {Button btn1 = null;    Button btn2 = null;super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); btn1 = (Button)findViewById(R.id.button1);     btn1.setOnClickListener(new Btn1Click());      btn2 = (Button)findViewById(R.id.button2);     btn2.setOnClickListener(new Btn2Click());}private final class Btn1Click implements View.OnClickListener {    public void onClick(View v) {    List<Person> persons;try {InputStream inputStream = new FileInputStream(new File(getApplicationContext().getFilesDir(), "person.xml"));persons = PersonService.getPersons(inputStream);for (Person person : persons){    Toast.makeText(getApplicationContext(), person.toString(), Toast.LENGTH_SHORT).show();    }} catch (Exception e) {e.printStackTrace();}    }    }private final class Btn2Click implements View.OnClickListener {public void onClick(View v) {List<Person> persons = new ArrayList<Person>();persons.add(new Person(44,"fuchangle",27));persons.add(new Person(55, "zhangyanfeng", 27));persons.add(new Person(66, "xuliang", 25));try {File xmlFile = new File(getApplicationContext().getFilesDir(), "person.xml");System.out.println(xmlFile.getPath());FileOutputStream outStream = new FileOutputStream(xmlFile);PersonService.save(persons, outStream);Toast.makeText(getApplicationContext(), "写入" + persons.toString(), Toast.LENGTH_LONG).show();} catch (FileNotFoundException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}}        }@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}


PersonService.java
import java.io.InputStream;import java.io.OutputStream;import java.util.ArrayList;import java.util.List;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlSerializer;import android.util.Xml;import com.app.bean.Person;public class PersonService {public static List<Person> getPersons(InputStream xml) throws Exception {List<Person> persons = null;Person person = null;XmlPullParser pullParser = Xml.newPullParser();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.valueOf(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.valueOf(pullParser.nextText());person.setAge(age);}break;case XmlPullParser.END_TAG:if ("person".equals(pullParser.getName())){persons.add(person);person = null;}break;}event = pullParser.next();}return persons;}/** * 写入文件 * @param persons * @param out * @throws Exception */public static void save(List<Person> persons, OutputStream out) throws Exception {XmlSerializer serializer = Xml.newSerializer();try {serializer.setOutput(out, "UTF-8");serializer.startDocument("UTF-8", true);serializer.startTag(null, "persons");for (Person person : persons) {serializer.startTag(null, "person");serializer.attribute(null, "id", person.getId().toString());serializer.startTag(null, "name");serializer.text(person.getName().toString());serializer.endTag(null, "name");serializer.startTag(null, "age");serializer.text(person.getAge().toString());serializer.endTag(null, "age");serializer.endTag(null, "person");}serializer.endTag(null, "persons");serializer.endDocument();} catch (Exception e) {e.printStackTrace();} finally {out.flush();out.close();}}}

更多相关文章

  1. Android的数据库--sqlite(一)
  2. Android(安卓)in Practice笔记第二章
  3. Android(安卓)MediaExtractor setDataSource
  4. android笔记
  5. Android(安卓)in Practice笔记第二章
  6. Android布局优化(四)X2C — 提升布局加载速度200%
  7. android布局文件中各属性所代表的意义
  8. Android(安卓)Jni代码示例讲解
  9. Android模拟SD卡实现方法解析

随机推荐

  1. Android单元测试与日志输出
  2. Android(安卓)读取本地(SD卡)图片
  3. android 3.2 bug in apn setting
  4. LinearLayout 特有属性
  5. Android(安卓)RTSP 视频处理
  6. Android主进程判断
  7. Android模拟器无法启动
  8. Android中获取网页表单中的数据
  9. Android(安卓)标签 (FlexboxLayout实现标
  10. Android(安卓)创建文件电脑端不显示