New To Java - Easiest way to get object --> XML --> object

Hello java gurus,

I'm trying to learn java and xml. Specifically, I want to serialize an object to XML, save it to a file, read it in again, and get back the same object. I would like to know the most pain-free way of doing this. I thought the following would work, but all it does it print an empty XMl file. Here's the code:

import java.beans.XMLEncoder;

import java.beans.XMLDecoder;

class Bicycle{

privateint speed = 0;

privateint gear = 1;

publicint getSpeed(){return speed;}

publicint getGear(){return gear;}

publicvoid setSpeed(int newValue){ this.speed = newValue;}

publicvoid setGear(int newValue){ this.gear = newValue;}

}

publicclass XMLDemo{

staticpublicvoid main(String[] args){

Bicycle bike =new Bicycle();

bike.setSpeed(10);

bike.setGear(7);

XMLEncoder xmle =new XMLEncoder(System.out);

xmle.writeObject(bike);

xmle.close();

}

}

When I run this, I get the following:

java XMLDemo

java.lang.IllegalAccessException: Class sun.reflect.misc.Trampoline can not access a member ofclass Bicycle with modifiers""

Continuing ...

java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(Bicycle);

Continuing ...

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

<java version="1.5.0_10" class="java.beans.XMLDecoder">

</java>

Is there something I'm doing wrong, or do I just misunderstand java XML usage all together?

Thanks for your help,

Michael

[3191 byte] By [msp3000a] at [2007-11-26 23:07:54]
# 1
I believe it's just saying it can only work with public classes. Try just making your Bicycle class public.
DrClapa at 2007-7-10 14:02:19 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks! That did indeed fix the problem.

Now I have a new problem -- I've lost output to System.out.

Here's my new code:

file #1: Bicycle.java

public class Bicycle {

private int speed = 0;

private int gear = 1;

public int getSpeed() { return speed; }

public int getGear() { return gear; }

public void setSpeed(int newValue) { this.speed = newValue; }

public void setGear(int newValue) { this.gear = newValue; }

}

file #2: XMLDemo.java

import java.beans.XMLEncoder;

import java.beans.XMLDecoder;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.IOException;

public class XMLDemo {

static public void main(String[] args) {

Bicycle bike = new Bicycle();

bike.setSpeed(10);

bike.setGear(7);

//

// Write XML to terminal

//

System.out.println("The XML code for an instance of Bicycle:");

XMLEncoder xmle = new XMLEncoder(System.out);

xmle.writeObject(bike);

//

// Write XML to a file

//

System.out.println("");

System.out.println("");

System.out.println("Saving XML to file: bicycle.xml");

XMLEncoder out = null;

try {

out = new XMLEncoder(

new BufferedOutputStream(

new FileOutputStream("bicycle.xml")

)

);

}

catch (Exception e) {

System.err.println("Cannot open output file: bicycle.xml");

}

try {

out.writeObject(bike);

}

catch (Exception e) {

System.err.println("Cannot write to file: bicycle.xml");

System.exit(1);

}

try {

out.close();

}

catch (Exception e) {

System.err.println("Cannot close output file: bicycle.xml");

System.exit(1);

}

//

// Read XML from a file

//

System.out.println("");

System.out.println("");

System.out.println("Reading XML from file: bicycle.xml");

XMLDecoder in = null;

try {

in = new XMLDecoder(

new BufferedInputStream(

new FileInputStream("bicycle.xml")

)

);

}

catch (Exception e) {

System.err.println("Cannot read from file: bicycle.xml");

System.exit(1);

}

Bicycle bike2 = null;

try {

bike2 = (Bicycle)in.readObject();

}

catch (Exception e) {

System.err.println("Cannot read file: bicycle.xml");

System.exit(1);

}

try {

in.close();

}

catch (Exception e) {

System.err.println("Cannot close input file: bicycle.xml");

System.exit(1);

}

}

}

The output is:

The XML code for an instance of Bicycle:

Saving XML to file: bicycle.xml

Reading XML from file: bicycle.xml

But, the output that I think I should get is:

The XML code for an instance of Bicycle:

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

<java version="1.5.0_10" class="java.beans.XMLDecoder">

<object class="Bicycle">

<void property="gear">

<int>7</int>

</void>

<void property="speed">

<int>10</int>

</void>

</object>

</java>

Saving XML to file: bicycle.xml

Reading XML from file: bicycle.xml

I assume that I'm just a clueless noob, but I can't find the cause. Why would XMLEncode stop writing to System.out in this version of my code if it worked in the previous version?

Thanks for all your help,

Michael

msp3000a at 2007-7-10 14:02:19 > top of Java-index,Java Essentials,New To Java...
# 3
xmle.writeObject(bike);Add an xmle.flush(); after this line.Also, don't forget to close() the writer at the end.
kevjavaa at 2007-7-10 14:02:19 > top of Java-index,Java Essentials,New To Java...
# 4
Thanks guys!
msp3000a at 2007-7-10 14:02:19 > top of Java-index,Java Essentials,New To Java...