把java對象轉化成xml檔案儲存

來源:互聯網
上載者:User

匯入的包
jettison-1.0.1.jar
xpp3_min-1.1.4c.jar
xstream-1.3.1.jar

userInfo:

package com.lb; public class userInfo { private String name;     private String phoneNumber;         private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String toString() { return "Name : "+this.name+        "\nPhoneNumber : "+this.phoneNumber+      "\nAddress : "+this.address;  } } 

userinfo2xml:

package com.lb; import java.io.FileNotFoundException; import java.io.FileOutputStream; import com.thoughtworks.xstream.XStream; import com.user.userInfo; public class userinfo2xml { public static void main(String[] args) { // 初始化BEAN對象 userInfo userInfo = new userInfo(); userInfo.setName("XXX"); userInfo.setPhoneNumber("XXXXXXXXX"); userInfo.setAddress("北京昌平區"); // 初始化XStream對象 XStream xStream = new XStream(); // xStream.omitField(Employee .class, "name"); //定義某一個屬性的值不進行xml序列化。 // xStream.useAttributeFor(String.class); // 對所有String類型的欄位定義為屬性tag顯示 // xStream.useAttributeFor("name",String.class);// 對所有String類型的欄位名成為name // 定義為屬性tag顯示,讀取的時候擷取不了該值 try { FileOutputStream fs = new FileOutputStream("d:/userInfo.xml"); xStream.toXML(userInfo, fs); } catch (FileNotFoundException e1) { e1.printStackTrace(); } } } 

 

聯繫我們

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