2.案例:
activity_main.xml
MainActivity.java
package com.example.zlf.a52;
import android.app.Activity;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Xml;
import android.view.View;
import android.widget.Toast;
import org.xmlpull.v1.XmlSerializer;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
private List userData; //1.首先创建了一个保存数据的集合
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 2.创建了集合的对象,模拟假数据
userData = new ArrayList();
for (int i = 0; i < 3; i++) {
userData.add(new Person("王" + i, 100 - i, 80 - i));
}
}
public void serializer(View view) {
try {
//3.创建一个XmlSerializer的对象
XmlSerializer serializer = Xml.newSerializer();
//4将文件保存在SD卡中,因此,需要创建一个file
File file = new File(Environment.getExternalStorageDirectory(),
"person.xml");
// 5需要设置文件的编码方式-输出流
FileOutputStream os = new FileOutputStream(file);
serializer.setOutput(os, "utf-8");
//写入XML文件标志
serializer.startDocument("utf-8", true);
//开始节点
serializer.startTag(null, "persons");
int count = 0;
for (Person person : userData) {
serializer.startTag(null, "person");
serializer.attribute(null, "id", count + "");
serializer.startTag(null, "name");
serializer.text(person.getName());
serializer.endTag(null, "name");
serializer.startTag(null, "age");
serializer.text(person.getAge()+"");
serializer.endTag(null, "age");
serializer.startTag(null, "score");
serializer.text(String.valueOf(person.getScore()));
serializer.endTag(null, "score");
serializer.endTag(null, "person");
count++;
}
serializer.endTag(null, "persons");
serializer.endDocument();
os.close();
Toast.makeText(this, "操作成功", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "操作失败", Toast.LENGTH_SHORT).show();
}
}
}
Person.java
package com.example.zlf.demo52_xml;
import android.content.Intent;
class Person {
private String name;
private Integer age;
private Integer score;
public Person(String name, Integer age, Integer score) {
super();
this.name=name;
this.age=age;
this.score=score;
}
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;
}
public void setScore(Integer score) {
this.score = score;
}
public Integer getScore() {
return score;
}
public String toString() {
return "Person [name=" + name + ", age=" + age + ", score=" + score
+ "]";
}
}
运行失败,出现的错误:
2018-05-03 09:57:14.858 6503-6503/? I/art: Not late-enabling -Xcheck:jni (already on)
2018-05-03 09:57:14.858 6503-6503/? W/art: Unexpected CPU variant for X86 using defaults: x86
2018-05-03 09:57:15.054 6503-6503/com.example.zlf.a52 W/System: ClassLoader referenced unknown path: /data/app/com.example.zlf.a52-1/lib/x86
2018-05-03 09:57:15.064 6503-6503/com.example.zlf.a52 I/InstantRun: starting instant run server: is main process
2018-05-03 09:57:15.258 6503-6521/com.example.zlf.a52 I/OpenGLRenderer: Initialized EGL, version 1.4
2018-05-03 09:57:15.258 6503-6521/com.example.zlf.a52 D/OpenGLRenderer: Swap behavior 1
2018-05-03 09:57:15.258 6503-6521/com.example.zlf.a52 W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2018-05-03 09:57:15.258 6503-6521/com.example.zlf.a52 D/OpenGLRenderer: Swap behavior 0
2018-05-03 09:57:15.325 6503-6521/com.example.zlf.a52 D/EGL_emulation: eglCreateContext: 0xa8b654c0: maj 2 min 0 rcv 2
2018-05-03 09:57:15.367 6503-6521/com.example.zlf.a52 D/EGL_emulation: eglMakeCurrent: 0xa8b654c0: ver 2 0 (tinfo 0x9eee35f0)
2018-05-03 09:57:15.393 6503-6521/com.example.zlf.a52 D/EGL_emulation: eglMakeCurrent: 0xa8b654c0: ver 2 0 (tinfo 0x9eee35f0)
除此之外虚拟机:
10:11 Emulator: audio: Failed to create voice `goldfish_audio'
10:11 Emulator: qemu-system-i386.exe: warning: opening audio output failed
10:11 Emulator: audio: Failed to create voice `goldfish_audio_in'
10:11 Emulator: qemu-system-i386.exe: warning: opening audio input failed
10:11 Emulator: audio: Failed to create voice `dac'
10:11 Emulator: audio: Failed to create voice `adc'
10:11 Emulator: audio: Failed to create voice `dac'
10:11 Emulator: audio: Failed to create voice `adc'
10:11 Emulator: getGeneralInfo: unrecoverable failure
写不出那个person.xml 文件,出错原因,没查到。