Serializing A Bean To XML

 
 
The XMLEncoder class serializes an object in similar fashion to java.io.ObjectOutput. However,unlike ObjectOutput,which persists all non-transient private and public data,the XMLEncoder only persists the value of public properties. In particular,for every public property,XMLEncoder calls its getter method and persists the returned value. In deserialization,a newly created object is initialized with these persisted property values. Therefore,any private state that is not associated with a property will,by default,not be persisted.
 
 
  1. // Create an object and set properties

  2.     MyClass o = new MyClass();

  3.     o.setProp(1);

  4.     o.setProps(new int[]{1,2,3});

  5.    

  6.     try {

  7.         // Serialize object into XML

  8.         XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(

  9.             new FileOutputStream("outfilename.xml")));

  10.         encoder.writeObject(o);

  11.         encoder.close();

  12.     } catch (FileNotFoundException e) {

  13.     }

  14.  

  15.     // This class defines two properties - prop and props

  16.     public class MyClass {

  17.         // The prop property

  18.         int i;

  19.         public int getProp() {

  20.             return i;

  21.         }

  22.         public void setProp(int i) {

  23.             this.i = i;

  24.         }

  25.    

  26.         // The props property

  27.         int[] iarray = new int[0];

  28.         public int[] getProps() {

  29.             return iarray;

  30.         }

  31.         public void setProps(int[] iarray) {

  32.             this.iarray = iarray;

  33.         }

  34.     }

  35.  

  36. Here is the XML data:

  37.     <?xml version="1.0" encoding="UTF-8"?>

  38.     <java version="1.4.0" class="java.beans.XMLDecoder">

  39.         <object class="MyClass">

  40.             <void property="prop">

  41.                 <int>1</int>

  42.             </void>

  43.             <void property="props">

  44.                 <array class="int" length="3">

  45.                     <void index="0">

  46.                         <int>1</int>

  47.                     </void>

  48.                     <void index="1">

  49.                         <int>2</int>

  50.                     </void>

  51.                     <void index="2">

  52.                         <int>3</int>

  53.                     </void>

  54.                 </array>

  55.             </void>

  56.         </object>

  57.     </java>
 
Copy to Clipboard      Download code as Text Format
 
  Date entered : 17th Feb 2007
  Rating : No Rating
  Accessed  :  5940
  Submitted by :  javabill
 
   Add Comment  Printer friendly
   View All Comments  Email to a friend
   Add to my Favourites Download PDF version
   Rating  
 
                 Click each image to add
this page to each site.
 
 
 
Comments Available

    No comment available at the moment.Be the first one to make a comment

 
Related JSP Source Codes
  • Spring
  • Wraps a string to a given number of characters using a string break character
  • Uppercase the first character of each word in a string
  • Make a string's first character uppercase
  • Strip whitespace (or other characters) from the beginning and end of a string

  • Copyright © 2013 VisualBuilder. All rights reserved

    Warning: Unknown(): write failed: Disk quota exceeded (122) in Unknown on line 0

    Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0