安卓如何產生xml檔案資料

來源:互聯網
上載者:User

標籤:產生xml檔案   如何   file   許可權   

注意這裡需要設定SD卡的許可權

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/54/6D/wKiom1SBwW-xDhQgAACasQh6_Cc145.jpg" title="QQ圖片20141205223036.jpg" alt="wKiom1SBwW-xDhQgAACasQh6_Cc145.jpg" />


public void onClick(View arg0) {

//建立序列化XML資料的對象

XmlSerializer xml=Xml.newSerializer();

//建立目標檔案對象

File file= new File(Environment.getExternalStorageDirectory(),"stuinfo");

//建立文檔輸出資料流對象

FileOutputStream fos=null;

try {

fos = new FileOutputStream(file);

//設定輸出時候的編碼集

xml.setOutput(fos, "utf-8");

//設定輸出文檔為獨立的XML

xml.startDocument("utf-8", true);

//設定根開始標籤

xml.startTag(null, "students");

//迴圈產生student元素

for(int i=0;i<list_name.size();i++)

{

xml.startTag(null, "student");

xml.startTag(null, "name");

xml.text(list_name.get(i));

xml.endTag(null, "name");

xml.startTag(null, "sex");

xml.text(list_sex.get(i));

xml.endTag(null, "sex");

xml.startTag(null, "age");

xml.text(list_age.get(i));

xml.endTag(null, "age");

xml.startTag(null, "tel");

xml.text(list_tel.get(i));

xml.endTag(null, "tel");

xml.endTag(null, "student");

}

//設定根結束標籤

xml.endTag(null, "students");

//設定文檔結束

xml.endDocument();

//釋放資源

fos.close();

Toast.makeText(MainActivity.this, "儲存成功", 100).show();

} catch (Exception e) {

Toast.makeText(MainActivity.this, "儲存失敗", 100).show();

}

}

});


本文出自 “安卓開發零基礎” 部落格,請務必保留此出處http://jinchao.blog.51cto.com/9651275/1586803

安卓如何產生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.