Hi,
I hope anyone here has used XMLEncoder sometime. Well I want to write an
object to an XML file with the encoder.
The object is in fact a bean but with several setter and getter methods
defined in superclasses. I have done introspection and it shows all the
properties nicely.
However, I am under the impression that XMLEncoder only writes out the
bean properties with getter and setter methods defined in the class
itself, and not in superclasses.
Is my assumption right? And if so, is there any solution to this
problem, because it's useless to me this way :((
Thanks a lot,
Steven
Chris Riesbeck - 12 Jan 2006 18:37 GMT
> Hi,
>
[quoted text clipped - 11 lines]
> Is my assumption right? And if so, is there any solution to this
> problem, because it's useless to me this way :((
Why assume when you can write code and test? Make 3 files and run
XMLTester. What do you see being printed?
import java.beans.XMLEncoder;
import java.io.*;
public class XMLTester {
public static void main(String[] args) {
XMLEncoder e = new XMLEncoder(System.out);
e.writeObject(new Baz(300, 400));
e.close();
}
}
public class Foo {
private int n = 100;
public int getFooValue() { return n; }
public void setFooValue(int i) { n = i; }
}
public class Baz extends Foo {
private int n = 200;
public Baz() {};
public Baz(int i, int j) { n = i; setFooValue(j); }
public int getBazValue() { return n; }
public void setBazValue(int i) { n = i; }
}