The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.beans  [12 examples] > Serialization  [4 examples]

e8. Deserializing a Bean from XML

See also e7 Serializing a Bean to XML.
    // Deserialize an object
    try {
        XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
            new FileInputStream("infilename.xml")));
    
        // MyClass is declared in e7 Serializing a Bean to XML
        MyClass o = (MyClass)decoder.readObject();
        decoder.close();
    
        // Use the object
        int prop = o.getProp();     // 1
        int[] props = o.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 Examples
e7. Serializing a Bean to XML
e9. Preventing a Bean Property from Being Serialized to XML
e10. Serializing an Immutable Bean Property to XML

See also: Events   


© 2002 Addison-Wesley.