XMLEncoder problem

T

Tomba

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
 
C

Chris Riesbeck

Tomba said:
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 :((

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; }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top