HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"XML Serialization of Java Objects"
Vol. 8, Issue 6, p. 34

	



Listing 1

XMLEncoder e = new XMLEncoder(os);
Person p = PersonFactory.createPerson("Smith");
Cust.setFirstName("John");
e.setPersistenceDelegate(Person.class, new DefaultPersistenceDelegate(){
  protected Expression instantiate(Object oldInstance, Encoder out){ 
   String lastName = ((Person)oldInstance).getLastName();
    return new Expression(
     oldInstance, 
     PersonFactory.class,
     "createPerson",
     new Object[] {lastName});
  }
 }
);
e.writeObject(p);

Listing 2

XMLEncoder e = new XMLEncoder(os);
Person p = new Person();
p.addNickname("Jonny");
p.addNickname("Jonboy"); 
e.setPersistenceDelegate(Person.class,new DefaultPersistenceDelegate(){
  protected void initialize(Class type,Object oldInstance, Object 
newInstance,Encoder out) {
   Person cst = (Person)oldInstance;
   Iterator iter = cst.getNicknames().iterator();
   while(iter.hasNext()){
     out.writeStatement(new Statement(
     oldInstance,
     "addNickname",
     new Object[] { iter.next() } ));
   }
  }
 });
e.writeObject(p);

Listing 3

<?xml version="1.0" encoding="UTF-8"?> 
<java version="1.4.1" class="java.beans.XMLDecoder"> 
 <object class="Person"> 
  <void method="addNickname"> 
   <string>Jonny</string> 
  </void> 
  <void method="addNickname"> 
   <string>Jonboy</string> 
  </void> 
 </object> 
</java> 

Listing 4

public PropertyDescriptor[] getPropertyDescriptors() {
 PropertyDescriptor[] result = 
  new PropertyDescriptor[] {
   new PropertyDescriptor("firstName",Person.class),
   new PropertyDescriptor("lastName",Person.class)
 };
 result[0].setValue("transient",Boolean.TRUE);
 return result;
}

 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: info@sys-con.com

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.