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?
XmlEncodercaptures 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/setXYZfor properties orisAbc/setAbcfor boolean properties, etc.) My guess is that the only property available like that in your class isName, 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.