I want to write/read an XML-File via JAXB using Java 1.7. In my Files I'm using StringProperty, ObservableList and SimpleObjectProperty. I have an xml adapter for each of this classes. When i try to marshall the classes i only get an neary empty xml-file.
ModelDescriptorHelper (stores the project list and marshal and unmarshall the xml file)
@XmlRootElement
public class ModelDescriptorHelper {
private static ModelDescriptorHelper helper = new ModelDescriptorHelper();
private File file;
@XmlTransient private ObservableList<Project> projects = FXCollections.observableArrayList();
private SimpleListProperty<Project> projectsProperty = new SimpleListProperty(this, "projects", projects);
public ObservableList<project> getProjects() {
return projectsProperty.get();
}
public void setCreoViewProjects(ObservableList<CreoViewProject> creoViewProjects) {
creoViewProjectsProperty.set(creoViewProjects);
}
public SimpleListProperty<CreoViewProject> creoViewProjectsProperty()
{
return creoViewProjectsProperty;
}
private ModelDescriptorHelper readData() {
try {
File file = this.file;
helper = JAXB.unmarshal(file, ModelDescriptorHelper.class);
helper.file = file;
} catch (Exception ex) {
}
return helper;
}
public void saveData() {
try {
if (!file.exists())
{
file.getParentFile().mkdirs();
}
else
{
file.delete();
file.createNewFile();
}
try (BufferedOutputStream bf = new BufferedOutputStream(new FileOutputStream(file, false))) {
JAXB.marshal(this, bf);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
@XmlTransient
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
readData();
}
public static ModelDescriptorHelper getInstance(File file) {
helper.setFile(file);
return helper;
}
}
Project Class:
public class Project {
private SimpleStringProperty wtContextName = new SimpleStringProperty();
private SimpleStringProperty projectName = new SimpleStringProperty();
public String getProjectName() {
return projectName.get();
}
public void setProjectName(String projectName) {
this.projectName.set(projectName);
}
public StringProperty projectNameProperty() {
return projectName;
}
public String getWtContextName() {
return wtContextName.get();
}
public void setWtContextName(String wtContextName) {
this.wtContextName.set(wtContextName);
}
public StringProperty wtContextNameProperty() {
return wtContextName;
}
}
StringPropertyAdapter:
public class SimpleStringPropertyAdatper extends XmlAdapter<String, SimpleStringProperty> {
@Override
public SimpleStringProperty unmarshal(String v) throws Exception {
return new SimpleStringProperty(v);
}
@Override
public String marshal(SimpleStringProperty v) throws Exception {
if(null == v) {
return null;
}
return v.get(); // Or whatever the correct method is
}
}
ObservableListAdatper:
public class ObservableListAdatper<T> extends XmlAdapter<LinkedList<T>, ObservableList<T>> {
@Override
public ObservableList<T> unmarshal(LinkedList<T> v) throws Exception {
return FXCollections.observableList(v);
}
@Override
public LinkedList<T> marshal(ObservableList<T> v) throws Exception {
LinkedList<T> list = new LinkedList();
list.addAll(v);
return list; // Or whatever the correct method is
}
}
Package info:
@XmlJavaTypeAdapters({
@XmlJavaTypeAdapter(value=SimpleStringPropertyAdatper.class, type=String.class),
@XmlJavaTypeAdapter(value=ObservableListAdatper.class, type=ObservableList.class)})
package org.project;
import javafx.collections.ObservableList;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;
Create Project and save data to xml:
ModelDescriptorHelper helper = ModelDescriptorHelper.getInstance(new File ("/test/test.xml"));
Project project = new Project();
project.setProjectName("Test Project");
helper.getProjects().add(project);
helper.getProjects().add(project);
helper.saveData();
The Xml file after marshall:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ModelDescriptorHelper>
<projects/>
</ModelDescriptorHelper>
When i debug it seems that the marshaller calls 2 times the ObservableListAdatper. 1 Time the list has 2 projects, second time the list is empty.
What do i wrong?