Java對象的XML序列化與反序列化實例解析
上一篇文章我們介紹了java實現(xiàn)的各種排序算法代碼示例,本文我們看看Java對象的xml序列化與反序列化的相關(guān)內(nèi)容,具體如下。
XML是一種標準的數(shù)據(jù)交換規(guī)范,可以方便地用于在應(yīng)用之間交換各類數(shù)據(jù)。如果能在Java對象和XML文檔之間建立某種映射,例如Java對象的XML序列化和反序列化,那么就可以使Java的對象方便地與其他應(yīng)用進行交換。
java.beans包里面有兩個類XMLEncoder和Decoder,分別用于將符合JabaBeans規(guī)范的Java對象以XML方式序列化和反序列化。以下代碼顯示了如何使用這兩個類實現(xiàn)Java對象的XML編碼和解碼。
待序列化的Java類:
import java.io.Serializable; public class SerialableObject implements Serializable { private static final long serialVersionUID = 8745578444312339136L; public SerialableObject() { } public SerialableObject(int id, String name, double value) { this.id = id; this.name = name; this.value = value; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getValue() { return value; } public void setValue(double value) { this.value = value; } private int id; private String name; private double value; }
XML序列化和反序列化用法演示類:
import java.beans.XMLDecoder; import java.beans.XMLEncoder; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import java.util.Vector; public class XmlSerialize { public XmlSerialize() { } public void serializeSingleObject(OutputStream os, Object obj) // 序列化單個java對象 { // XMLEncoder xe = new XMLEncoder(os); XMLEncoder xe = new XMLEncoder(os, "GBK", true, 0); // 僅用于Java SE 7 xe.writeObject(obj); // 序列化成XML字符串 xe.close(); } public Object deserializeSingleObject(InputStream is) // 反序列化單個Java對象 { XMLDecoder xd = new XMLDecoder(is); Object obj = xd.readObject(); // 從XML序列中解碼為Java對象 xd.close(); return obj; } public void serializeMultipleObject(OutputStream os, List<Object> objs) // 序列化多個Java對象 { XMLEncoder xe = new XMLEncoder(os); xe.writeObject(objs); // 序列化成XML字符串 xe.close(); } public List<Object> deserializeMultipleObject(InputStream is) // 反序列化多個Java對象 { XMLDecoder xd = new XMLDecoder(is); @SuppressWarnings("unchecked") List<Object> objs = (List<Object>)xd.readObject(); // 從XML序列中解碼為Java對象列表 xd.close(); return objs; } public void runSingleObject() { File xmlFile = new File("object.xml"); SerialableObject jo4Out = new SerialableObject(1, "Java序列化為XML", 3.14159265359); // 創(chuàng)建待序列化的對象 try { FileOutputStream ofs = new FileOutputStream(xmlFile); // 創(chuàng)建文件輸出流對象 serializeSingleObject(ofs, jo4Out); ofs.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { FileInputStream ifs = new FileInputStream(xmlFile); SerialableObject jo4In = (SerialableObject)deserializeSingleObject(ifs); System.out.println("id: " + jo4In.getId()); System.out.println("name: " + jo4In.getName()); System.out.println("value: " + jo4In.getValue()); } catch (FileNotFoundException e) { e.printStackTrace(); } } public void runMultipleObject() { File xmlFile = new File("objects.xml"); List<SerialableObject> sos4Out = new Vector<SerialableObject>(); sos4Out.add(new SerialableObject(1, "Java序列化為XML - 1", 3.14)); // 創(chuàng)建待序列化的對象 sos4Out.add(new SerialableObject(2, "Java序列化為XML - 2", 3.14159)); // 創(chuàng)建待序列化的對象 sos4Out.add(new SerialableObject(3, "Java序列化為XML - 3", 3.1415926)); // 創(chuàng)建待序列化的對象 sos4Out.add(new SerialableObject(4, "Java序列化為XML - 4", 3.141592653)); // 創(chuàng)建待序列化的對象 sos4Out.add(new SerialableObject(5, "Java序列化為XML - 5", 3.14159265359)); // 創(chuàng)建待序列化的對象 try { FileOutputStream ofs = new FileOutputStream(xmlFile); // 創(chuàng)建文件輸出流對象 serializeSingleObject(ofs, sos4Out); ofs.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { FileInputStream ifs = new FileInputStream(xmlFile); @SuppressWarnings("unchecked") List<SerialableObject> sos4In = (List<SerialableObject>)deserializeSingleObject(ifs); for (SerialableObject jo4In : sos4In) { System.out.println("id: " + jo4In.getId()); System.out.println("name: " + jo4In.getName()); System.out.println("value: " + jo4In.getValue()); } } catch (FileNotFoundException e) { e.printStackTrace(); } } public static void main(String[] args) { XmlSerialize xs = new XmlSerialize(); xs.runSingleObject(); xs.runMultipleObject(); } }
需要注意的是,待序列化的類必須要符合JavaBeans的格式規(guī)范,即:具有一個無參的public構(gòu)造函數(shù),所有數(shù)據(jù)成員的訪問均采用getter/setter模式,此外,這個類必須是public的,并且實現(xiàn)了java.io.Serializable接口。
程序運行之后,會產(chǎn)生兩個文件:
object.xml是runSingleObject方法生成的,存放了單個的SerialableObject的值:
<?xml version="1.0" encoding="GBK"?> <java version="1.7.0" class="java.beans.XMLDecoder"> <object class="SerialableObject"> <void property="id"> <int>1</int> </void> <void property="name"> <string>Java序列化為XML</string> </void> <void property="value"> <double>3.14159265359</double> </void> </object> </java>
objects.xml是runMultipleObject方法產(chǎn)生的,存放了5個SerializableObject的值:
<?xml version="1.0" encoding="GBK"?> <java version="1.7.0" class="java.beans.XMLDecoder"> <object class="java.util.Vector"> <void method="add"> <object class="SerialableObject"> <void property="id"> <int>1</int> </void> <void property="name"> <string>Java序列化為XML - 1</string> </void> <void property="value"> <double>3.14</double> </void> </object> </void> <void method="add"> <object class="SerialableObject"> <void property="id"> <int>2</int> </void> <void property="name"> <string>Java序列化為XML - 2</string> </void> <void property="value"> <double>3.14159</double> </void> </object> </void> <void method="add"> <object class="SerialableObject"> <void property="id"> <int>3</int> </void> <void property="name"> <string>Java序列化為XML - 3</string> </void> <void property="value"> <double>3.1415926</double> </void> </object> </void> <void method="add"> <object class="SerialableObject"> <void property="id"> <int>4</int> </void> <void property="name"> <string>Java序列化為XML - 4</string> </void> <void property="value"> <double>3.141592653</double> </void> </object> </void> <void method="add"> <object class="SerialableObject"> <void property="id"> <int>5</int> </void> <void property="name"> <string>Java序列化為XML - 5</string> </void> <void property="value"> <double>3.14159265359</double> </void> </object> </void> </object> </java>
總結(jié)
以上就是本文關(guān)于Java對象的XML序列化與反序列化實例解析的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:Java編程redisson實現(xiàn)分布式鎖代碼示例、Java編程ssh整合常見錯誤解析等,有什么問題可以隨時留言,小編會及時回復(fù)大家的。下面推薦幾本Java編程相關(guān)的書籍,供大家參考,免費下載哦:
寫給大忙人看的Java核心技術(shù) ([美]凱·S·霍斯特曼) 中文pdf掃描版
http://chabaoo.cn/books/556994.html
java Vector類源代碼與分析 WORD版
http://chabaoo.cn/books/549902.html
希望大家能夠喜歡,更多精彩內(nèi)容盡在:http://chabaoo.cn/
- java中xml和對象之間的互相轉(zhuǎn)換方法
- java利用JAXB實現(xiàn)對象和xml互相轉(zhuǎn)換方法與實例詳解
- 通過實例學(xué)習(xí)JAVA對象轉(zhuǎn)成XML輸出
- 將Java對象序列化成JSON和XML格式的實例
- xml與Java對象的轉(zhuǎn)換詳解
- XML到Java代碼的數(shù)據(jù)綁定之對象
- Javabean基于xstream包實現(xiàn)轉(zhuǎn)XML文檔的方法
- 使用asx3m與xstream配合解決flex與java利用httpservice傳遞xml數(shù)據(jù)問題
- java使用xstream實現(xiàn)xml文件和對象之間的相互轉(zhuǎn)換
相關(guān)文章
idea設(shè)置@Author文件頭注釋的實現(xiàn)步驟
本文主要介紹了idea設(shè)置@Author文件頭注釋的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07spring cloud gateway 如何修改請求路徑Path
這篇文章主要介紹了spring cloud gateway 修改請求路徑Path的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06JAVA CountDownLatch與thread-join()的區(qū)別解析
這篇文章主要介紹了JAVA CountDownLatch與thread-join()的區(qū)別解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-08-08Spring?Boot中使用Swagger3.0.0版本構(gòu)建RESTful?APIs的方法
Swagger?是一個規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化?RESTful?風(fēng)格的?Web?服務(wù),這篇文章主要介紹了Spring?Boot中使用Swagger3.0.0版本構(gòu)建RESTful?APIs的方法,需要的朋友可以參考下2022-11-11