您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
23_生成xml文件
发布时间:2021-02-11 17:11:13编辑:雪饮阅读()
写入xml到文件(sdcard)
首先写入xml到文件,这里因为要写入到sdcard中,那么需要权限WRITE_EXTERNAL_STORAGE
所以要在清单文件AndroidManifest.xml中加入权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
然后创建一个用于写入数据到xml文件的方法PersonService.java如:
package com.example.myapplication;
import android.content.Context;
import android.os.Environment;
import android.util.Xml;
import org.xmlpull.v1.XmlSerializer;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
public class PersonService {
private Context context;
public PersonService(Context context) {
this.context = context;
}
/**
* 把persons集合里面的内容写到xml文件里面
* @param persons person的集合
* @return
*/
public boolean savePersonToXml(List<Person> persons){
try {
//xml序列化实例化
XmlSerializer serializer = Xml.newSerializer();
File file = new File(Environment.getExternalStorageDirectory(),"person.xml");
FileOutputStream fos = new FileOutputStream(file);
//以uft-8输出
serializer.setOutput(fos, "utf-8");
//开始标签(xml文档头),true表示该xml为独立xml,不依赖其它
serializer.startDocument("utf-8", true);
//根标签的开始标签,命名空间不设置(null);标签名
//所谓的命名空间就类似如:
/*
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication">,
这里的xmlns就是命名空间
*/
serializer.startTag(null, "persons");
for(Person person:persons){
serializer.startTag(null, "person");
//属性也有命名空间,这里不设置命名空间(null),这里id属性需要string类型,所以要拼接一个空字串,不过我觉得这样写有点low
serializer.attribute(null, "id", person.getId()+"");
serializer.startTag(null,"name");
//设置文本节点内容(这里是name标签的文本内容)
serializer.text(person.getName());
//设置某个标签的结束标签,同样有命名空间。。。
serializer.endTag(null, "name");
serializer.startTag(null,"age");
serializer.text(person.getAge()+"");
serializer.endTag(null, "age");
serializer.endTag(null, "person");
}
serializer.endTag(null, "persons");
//xml文档尾
serializer.endDocument();
fos.flush();
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
然后建立用于数据中转的Person.java
package com.example.myapplication;
public class Person {
private int id;
private String name;
private int age;
public Person(int id, String name, int age) {
this.id = id;
this.name = name;
this.age=age;
}
public int getId() {
return id;
}
public String getName() {
return this.name;
}
public int getAge() {
return age;
}
}
然后布局文件中只需要建立一个按钮,并在对应java控制器文件中建立对应点击事件onclick来执行对应写入数据到xml文件中
布局文件activity_main.xml(调用send)如:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="写入到xml文件"
android:id="@+id/send_button"
android:onClick="send"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
然后在控制器中实现send方法, MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View view) {
PersonService service = new PersonService(this);
List<Person> persons = new ArrayList<Person>();
Person p1 = new Person(21, "张三", 18);
Person p2 = new Person(27, "李四", 29);
Person p3 = new Person(29, "wangwu", 3);
persons.add(p1);
persons.add(p2);
persons.add(p3);
service.savePersonToXml(persons);
}
}
然后部署项目到设备中在设备的app中先允许(勾选)其可以写入数据到存储的权限,然后点击app主界面的按钮即可写入数据到sdcard中
其写入文件的路径一般在sdcard卡的根目录
关键字词:android,xml