android读写XML

详细代码看附件!

布局文件:

<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;
}
@Override
public 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 {

@Override
protected 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();
}
}

}

@Override
public 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();
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值