Reputation: 5543
I'm trying to put into XML-file some data. Here is the code:
@Test
public void TestProximities() {
// Create a proximity
ISpaceComponent solarSystem = Init.createSolarSystemProximities();
solarSystem.runTtest();
Path filePath = Paths.get("c:\\temp\\junk.bin");
File xmlFile = new File("c:\\temp\\");
boolean success = xmlFile.mkdirs();
if (!success && ! xmlFile.exists() ) {
// Directory creation failed
System.out.println("Failed to create a file: " + filePath);
}
try {
XMLEncoder fileXml = new XMLEncoder( new BufferedOutputStream( new FileOutputStream("c:\\temp\\junk.xml") ) );
fileXml.writeObject(solarSystem);
fileXml.flush();
fileXml.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
Init.createSolarSystemProximities():
public static ISpaceComposite createSolarSystemProximities() {
// Proximities
ISpaceComposite solarSystem = new SpaceComposite("The Solar System");
solarSystem.addComponent(new Star("Sol"));
ISpaceComposite MercuryProximity = new SpaceComposite("Mercury proximity");
MercuryProximity.addComponent(new Planet("Mercury"));
solarSystem.addComponent(MercuryProximity);
ISpaceComposite VenusProximity = new SpaceComposite("Venus proximity");
VenusProximity.addComponent(new Planet("Venus"));
solarSystem.addComponent(VenusProximity);
ISpaceComposite EarthProximity = new SpaceComposite("Earth proximity");
EarthProximity.addComponent(new Planet("The Earth"));
EarthProximity.addComponent(new Moon("The Moon"));
solarSystem.addComponent(EarthProximity);
ISpaceComposite MarsProximity = new SpaceComposite("Mars proximity");
MarsProximity.addComponent(new Planet("Mars"));
MarsProximity.addComponent(new Moon("Phobos"));
MarsProximity.addComponent(new Moon("Deimos"));
solarSystem.addComponent(MarsProximity);
return solarSystem;
}
And the space composite class is looking like this:
public class SpaceComposite extends SpaceComponent implements ISpaceComposite {
public static final long serialVersionUID = Data.serialVersionUID;
private ArrayList<ISpaceComponent> _components;
private int _currentMoonIndex;
}
Also 'SpaceComposite' has default prameterless constructor. But the only thing, which is inserted into a file, is its name, derrived from the 'SpaceComponent' class
public abstract class SpaceComponent implements ISpaceComponent, Serializable{
private String _title;
public static final long serialVersionUID = Data.serialVersionUID;
// Default constructor
public SpaceComponent(){
_title = "";
}
}
How can I solve this problem?
Upvotes: 2
Views: 1164
Reputation: 726649
XmlEncoder
captures properties using JavaBean conventions. It works by cloning the object graph and recording the steps that were necessary to create the clone. A class that you would like to use with XmlEncoder without any customizations needs to follow JavaBeans conventions (getXYZ/setXYZ
for properties or isAbc/setAbc
for boolean properties, etc.) My guess is that the only property available like that in your class is Name
, hence it's the only one being serialized.
The article at the link also explains how you can customize the process by providing your own persistence delegate, but I would try adding getters/setters first to see if that is enough to make it work.
Upvotes: 1