生成XML的方式: 第一種:利用StringBuffer強行拼接 第二種:通過XmlSerializer方式(推薦) 下邊是示例: 一個簡單的界面: 一個Javabean: 看一下生成的XML: 成功! ...
生成XML的方式:
第一種:利用StringBuffer強行拼接
第二種:通過XmlSerializer方式(推薦)
下邊是示例:
一個簡單的界面:
<RelativeLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:onClick="click1" android:text="生成XML1" /> <Button android:layout_below="@id/button1" android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:onClick="click2" android:text="生成XML2" /> </RelativeLayout>
一個Javabean:
package org.dreamtech.xml; public class Sms { private String address; private String body; private String date; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } }
package org.dreamtech.xml; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlSerializer; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.util.Xml; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { private List<Sms> smsLists; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); smsLists = new ArrayList<Sms>(); for (int i = 0; i < 10; i++) { Sms sms = new Sms(); sms.setAddress("No." + i); sms.setBody("hello,No." + i); sms.setDate("201" + i); smsLists.add(sms); } } // 點擊按鈕通過StringBuffer的方式生成一個XML文件 public void click1(View v) { StringBuffer sb = new StringBuffer(); // 組拼XML的頭 sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); // 根節點 sb.append("<smss>"); // sms節點 for (Sms sms : smsLists) { sb.append("<sms>"); // 組拼數據 sb.append("<address>"); sb.append(sms.getAddress()); sb.append("</address>"); sb.append("<body>"); sb.append(sms.getBody()); sb.append("</body>"); sb.append("<date>"); sb.append(sms.getDate()); sb.append("</date>"); sb.append("</sms>"); } sb.append("</smss>"); // 將數據保存到sdcard中(註意配置中加入許可權) try { File file = new File(Environment.getExternalStorageDirectory() .getPath(), "smsbackup1.xml"); FileOutputStream fos = new FileOutputStream(file); fos.write(sb.toString().getBytes()); fos.close(); Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_LONG) .show(); } catch (Exception e) { e.printStackTrace(); } } // 通過XmlSerializer方式(推薦) public void click2(View v) { try { XmlSerializer serializer = Xml.newSerializer(); File file = new File(Environment.getExternalStorageDirectory() .getPath(), "smsbackup2.xml"); FileOutputStream fos = new FileOutputStream(file); serializer.setOutput(fos, "utf-8"); // 第二個參數:是否是獨立的XML文件,選True即可 serializer.startDocument("utf-8", true); // 節點 serializer.startTag(null, "smss"); for (Sms sms : smsLists) { serializer.startTag(null, "sms"); serializer.startTag(null, "address"); serializer.text(sms.getAddress()); serializer.endTag(null, "address"); serializer.startTag(null, "body"); serializer.text(sms.getBody()); serializer.endTag(null, "body"); serializer.startTag(null, "date"); serializer.text(sms.getDate()); serializer.endTag(null, "date"); serializer.endTag(null, "sms"); } serializer.endTag(null, "smss"); serializer.endDocument(); Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_LONG) .show(); } catch (Exception e) { e.printStackTrace(); } } }
看一下生成的XML:
成功!