android之XmlSerializer序列化XML(寫入)

來源:互聯網
上載者:User

      首先,我們看一下什麼是serializer,serializer就是序列化,又名序列化。它可並不只是簡單的把對象儲存在儲存空間上,它可以使我們在流中傳輸對象,使對象變的可以像基本資料一樣傳遞。

最終如上

現在粘貼主要代碼:

main.xml
<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /><TextView<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:id="@+id/textView"<br /> /><br /></LinearLayout><br />

activity的代碼

 

package cn.com.xmlseriliazer;</p><p>import java.io.StringWriter;<br />import java.util.ArrayList;</p><p>import org.xmlpull.v1.XmlPullParserFactory;<br />import org.xmlpull.v1.XmlSerializer;</p><p>import android.app.Activity;<br />import android.os.Bundle;<br />import android.widget.TextView;<br />/**<br /> *<br /> * @author chenzheng_java<br /> * @description 測試通過XmlSerilizer產生xml檔案<br /> * @since 2011/03/03<br /> *<br /> */<br />public class XmlSerializerActivity extends Activity {<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> String result = produceXml();<br /> TextView textView = (TextView)this.findViewById(R.id.textView);<br /> textView.setText(result);</p><p> }<br /> /**<br /> *<br /> * @return 產生的xml檔案的字串表示<br /> */<br /> private String produceXml(){</p><p> StringWriter stringWriter = new StringWriter();<br /> ArrayList<Beauty> beautyList = getData();<br /> try {<br /> // 擷取XmlSerializer對象<br />XmlPullParserFactory factory = XmlPullParserFactory.newInstance();<br />XmlSerializer xmlSerializer = factory.newSerializer();<br />// 設定輸出資料流對象<br />xmlSerializer.setOutput(stringWriter);<br />/*<br /> * startDocument(String encoding, Boolean standalone)encoding代表編碼方式<br /> * standalone 用來表示該檔案是否呼叫其它外部的檔案。<br /> * 若值是 ”yes” 表示沒有呼叫外部規則檔案,若值是 ”no” 則表示有呼叫外部規則檔案。預設值是 “yes”。<br /> */<br />xmlSerializer.startDocument("utf-8", true);<br />xmlSerializer.startTag(null, "beauties");<br />for(Beauty beauty:beautyList){<br />/*<br /> * startTag (String namespace, String name)這裡的namespace用於唯一標識xml標籤<br /> *XML 命名空間屬性被放置於某個元素的開始標籤之中,並使用以下的文法:<br />xmlns:namespace-prefix="namespaceURI"<br />當一個命名空間被定義在某個元素的開始標籤中時,所有帶有相同首碼的子項目都會與同一個命名空間相關聯。<br />注釋:用於標示命名空間的地址不會被解析器用於尋找資訊。其惟一的作用是賦予命名空間一個惟一的名稱。不過,很多公司常常會作為指標來使用命名空間指向某個實存的網頁,這個網頁包含著有關命名空間的資訊。<br /> */<br />xmlSerializer.startTag(null, "beauty");</p><p>xmlSerializer.startTag(null, "name");<br />xmlSerializer.text(beauty.getName());<br />xmlSerializer.endTag(null, "name");</p><p>xmlSerializer.startTag(null, "age");<br />xmlSerializer.text(beauty.getAge());<br />xmlSerializer.endTag(null, "age");</p><p>xmlSerializer.endTag(null, "beauty");<br />}<br />xmlSerializer.endTag(null, "beauties");<br />xmlSerializer.endDocument();<br />} catch (Exception e) {<br />e.printStackTrace();<br />}<br />return stringWriter.toString();</p><p> }</p><p> /**<br /> *<br /> * @return 包含了眾多美女資訊的集合<br /> */<br /> private ArrayList<Beauty> getData(){<br /> ArrayList<Beauty> beautyList = new ArrayList<Beauty>();</p><p> Beauty yangmi = new Beauty("楊冪", "23");<br /> Beauty linzhiling = new Beauty("林志玲", "28");</p><p> beautyList.add(yangmi);<br /> beautyList.add(linzhiling);</p><p> return beautyList;<br /> }</p><p> /**<br /> *<br /> * @author chenzheng_java<br /> *美人實體類<br /> */<br /> private class Beauty{<br /> String name;<br /> String age ;<br />public String getName() {<br />return name;<br />}<br />public void setName(String name) {<br />this.name = name;<br />}<br />public String getAge() {<br />return age;<br />}<br />public void setAge(String age) {<br />this.age = age;<br />}<br />@Override<br />public String toString() {<br />return "Beauty [age=" + age + ", name=" + name + "]";<br />}<br />public Beauty(String name, String age) {<br />this.name = name;<br />this.age = age;<br />}<br />public Beauty(){</p><p>}</p><p> }</p><p>} 

其他都為預設。

 由代碼我們可以看到,其實使用xmlserializer產生xml也是相當容易的。基本的步驟和解析xml差不多。這裡就不多說話了。詳情請看api。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.