|
How to deserialize a bean from XML |
|
|
This Java tip illustrates a method of deserialize a bean from XML. For developer information,
when a Bean instance is serialized, it is converted into a data stream and written to storage.
Any applet, application, or tool that uses that Bean can then "reconstitute" it by deserialization.
// Deserialize an object
try {
XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
new FileInputStream("infilename.xml")));
MyClass obj = (MyClass)decoder.readObject();
decoder.close();
// Use the object
int property = obj.getProp(); // 1
int[] properties = obj.getProps(); // [1, 2, 3]
} catch (FileNotFoundException e) {
}
|
Here is the XML data being deserialized:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.0" class="java.beans.XMLDecoder">
<object class="MyClass">
<void property="prop">
<int>1</int>
</void>
<void property="props">
<array class="int" length="3">
<void index="0">
<int>1</int>
</void>
<void index="1">
<int>2</int>
</void>
<void index="2">
<int>3</int>
</void>
</array>
</void>
</object>
</java>
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.