jastrachan | 6 Apr 16:28 2004
Picon

Re: java.beans.XMLEncoder with custom bean not working

For some reason the XMLEncoder doesn't like it running inside the 
command line shell. Though if I added this script...

obj = new Car ( make:"Jeep", model: "Laredo" );

fname = "cartest.xml";
enc = new java.beans.XMLEncoder ( new java.io.FileOutputStream ( fname
) );
enc.writeObject( obj ); enc.close();

class Car implements java.io.Serializable {
       String make
       String model
     }

to a text file and ran it via the groovy command line

     groovy foo.groovy

then it worked fine, creating this file...

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.4.2_03" class="java.beans.XMLDecoder">
  <object class="Car">
   <void property="make">
    <string>Jeep</string>
   </void>
   <void property="model">
    <string>Laredo</string>
   </void>
  </object>
</java>

I confess to not understanding why - I suspect there's a ClassLoader 
issue going on here. Maybe we need to set the 
Thread.currentThread().getContextClassLoader() or something?

On 6 Apr 2004, at 14:31, Kas Thomas wrote:
> Maybe someone can tell me whether I've encountered a bug, a feature, or
> mere user insanity... ;^)
>
> The following doesn't quite work as expected:
>
> class Car implements java.io.Serializable {
>       String make
>       String model
>     }
> obj = new Car ( make:"Jeep", model: "Laredo" );
>
> fname = "d:\\dev\\cartest.xml";
> enc = new java.beans.XMLEncoder ( new java.io.FileOutputStream ( fname
> ) );
> enc.writeObject( obj ); enc.close();
>
>
>
> The above code, when executed in the groovy Swing console, gives a
> ClassNotFoundException on Car, and an XML file containing:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <java version="1.4.2-beta" class="java.beans.XMLDecoder">
> </java>
>
>
> The ClassNotFoundException seems strange, because the Car instance is
> there, I can instantiate more Cars in the same console session, I can
> inspect properties on any instance, etc.
>
> The Groovy doc uses the term "bean" rather generally to describe user
> objects like Car, and I therefore expected XMLEncoder to work on a Car
> instance... but no dice.
>
> A nice feature for Groovy would be some kind of very easy, tight,
> seamless support for XMLEncoder/XMLDecoder on beans such that one can
> just do something like:
>
> obj = new Car ( make:"Jeep", model: "Laredo" );
> filename = "mycar.xml";
> groovy.xmlEncode(obj, filename);
>
>
> --KT
>
> _______________________________________________
> groovy-dev mailing list
> groovy-dev@...
> http://lists.codehaus.org/mailman/listinfo/groovy-dev
>
>

James
-------
http://radio.weblogs.com/0112098/

Gmane