Android中常用的XML生成方法實例分析
更新時間:2015年10月09日 11:32:08 作者:antkingwei
這篇文章主要介紹了Android中常用的XML生成方法,以實例形式較為詳細(xì)的分析了Android生成XML的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Android中常用的XML生成方法。分享給大家供大家參考。具體如下:
1. java代碼:
package com.android.antking.xml;
import java.io.OutputStream;
import java.util.List;
import org.xmlpull.v1.XmlSerializer;
import android.util.Xml;
/**采用pull 生成xml文件
*
* @author antkingwei
*
*/
public class PullBuildXMLService {
public void buildXML(List<Person> persons,OutputStream outputStream)throws Exception{
XmlSerializer serializer = Xml.newSerializer();
serializer.setOutput(outputStream,"utf-8");
serializer.startDocument("utf-8", true);
serializer.startTag(null, "perisons");
for(Person person:persons){
serializer.startTag(null, "perison");
serializer.attribute(null, "id",String.valueOf(person.id));
serializer.startTag(null, "name");
serializer.text(person.name);
serializer.endTag(null, "name");
serializer.startTag(null, "age");
serializer.text(String.valueOf(person.age));
serializer.endTag(null, "age");
serializer.endTag(null, "perison");
}
serializer.endTag(null, "perisons");
serializer.endDocument();
outputStream.close();
}
}
2. java ben:
package com.android.antking.xml;
public class Person {
public int id;
public String name;
public int age;
}
3. 調(diào)用方法:
public void writeFile() throws Throwable{
List<Person> list = new ArrayList<Person>();
for(int i =0;i<10;i++){
Person person = new Person();
person.id = 1;
person.name = "ant";
person.age = 12;
list.add(person);
}
File file = new File(MainActivity.this.getFilesDir(),"person.xml");
FileOutputStream outputStream = new FileOutputStream(file);
PullBuildXMLService service = new PullBuildXMLService();
service.buildXML(list, outputStream);
}
希望本文所述對大家的Android程序設(shè)計有所幫助。
相關(guān)文章
Android bindService的使用與Service生命周期案例詳解
這篇文章主要介紹了Android bindService的使用與Service生命周期案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
Android Force Close 出現(xiàn)的異常原因分析及解決方法
本文給大家講解Android Force Close 出現(xiàn)的異常原因分析及解決方法,forceclose意為強行關(guān)閉,當(dāng)前應(yīng)用程序發(fā)生了沖突。對android force close異常分析感興趣的朋友一起通過本文學(xué)習(xí)吧2016-08-08
Android通過Socket與服務(wù)器之間進行通信的示例
這篇文章主要介紹了Android通過Socket與服務(wù)器之間進行通信的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
Android Http協(xié)議訪問網(wǎng)絡(luò)實例(3種)
本篇文章主要介紹了Android Http協(xié)議訪問網(wǎng)絡(luò)實例(3種),具有一定的參考價值,有興趣的可以了解一下2017-07-07
詳解Android PopupWindow怎么合理控制彈出位置(showAtLocation)
本篇文章主要介紹了詳解Android PopupWindow怎么合理控制彈出位置(showAtLocation),具有一定的參考價值,有興趣的可以了解一下2017-10-10

