XML檔案產生的幾個步驟

來源:互聯網
上載者:User

標籤:xml

1、得到xml序列化器
XmlSerializer serializer = Xml.newSerializer();

 

2、確定要產生的xml檔案的存放的位置,將流對象對應到檔案儲存體的位置
File file = new File(Environment.getExternalStorageDirectory(), "backup.xml");
FileOutputStream os = new FileOutputStream(file);

 

3、設定xml序列化器的輸入資料流對象和編碼格式
serializer.setOutput(os, "utf-8");

 

4、設定xml檔案的聲明資訊
serializer.startDocument("utf-8", true);

 

5、設定xml檔案的根標籤
serializer.startTag(null, "smss");

 

6、設定每一個對象的開始標籤
for (SmsInfo info : smsInfos)
{
 serializer.startTag(null, "sms");
 serializer.attribute(null, "id", info.getId() + "");//設定標籤的屬性資訊
 
 serializer.startTag(null, "address");
 serializer.text(info.getAddress());
 serializer.endTag(null, "address");

 serializer.startTag(null, "type");
 serializer.text(info.getType() + "");
 serializer.endTag(null, "type");

 serializer.startTag(null, "body");
 serializer.text(info.getBody());
 serializer.endTag(null, "body");

 serializer.startTag(null, "date");
 serializer.text(info.getDate() + "");
 serializer.endTag(null, "date");

 serializer.endTag(null, "sms");
}

 

7、設定xml檔案的結束標籤
serializer.endTag(null, "smss");

 

8、設定xml檔案的接受資訊
serializer.endDocument();

 

9、關閉檔案的寫入流
os.close();

 

下面是產生的xml檔案

<smss>
    <sms id="0">
        <address>13500000000</address>
        <type>1</type>
        <body>簡訊內容0</body>
        <date>1415199223241</date>
    </sms>
    <sms id="1">
        <address>13500000001</address>
        <type>1</type>
        <body>簡訊內容1</body>
        <date>1415199223241</date>
     </sms>
</smss>
 

XML檔案產生的幾個步驟

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.