java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4090
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to serialize a bean to XML E-mail
User Rating: / 16
PoorBest 

This Java tip demonstrates a method of serializing a bean to XML. A Bean persists by having its properties, fields, and state information saved and restored to and from storage. The mechanism that makes persistence possible is called serialization. JavaBeans uses the JDK's object serialization API for its serialization needs.

    // Create an object and set properties
    MyClass obj = new MyClass();
    obj.setProp(1);
    obj.setProps(new int[]{123});
    
    try {

        // Serialize object into XML
        XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
            new FileOutputStream("outfilename.xml")));
        encoder.writeObject(o);
        encoder.close();

    catch (FileNotFoundException e) {
    }

    // This class defines two properties - prop and props
    public class MyClass {

        // The prop property
        int i;
        public int getProp() {
            return i;
        }
        public void setProp(int i) {
            this.i = i;
        }
    
        // The props property
        int[] iarray = new int[0];

        public int[] getProps() {
            return iarray;
        }
        public void setProps(int[] iarray) {
            this.iarray = iarray;
        }
    }

Here is the XML data:

    <?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

 
< Prev

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.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.