ObservableList and StringProperty are not marshalled in xml file

demonator

New Member
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)\[code\]@XmlRootElementpublic 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(); }}@XmlTransientpublic File getFile() { return file;}public void setFile(File file) { this.file = file; readData();}public static ModelDescriptorHelper getInstance(File file) { helper.setFile(file); return helper;}}\[/code\]Project Class:\[code\]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;}}\[/code\]StringPropertyAdapter:\[code\]public class SimpleStringPropertyAdatper extends XmlAdapter<String, SimpleStringProperty> {@Overridepublic SimpleStringProperty unmarshal(String v) throws Exception { return new SimpleStringProperty(v);}@Overridepublic String marshal(SimpleStringProperty v) throws Exception { if(null == v) { return null; } return v.get(); // Or whatever the correct method is} \[/code\]}ObservableListAdatper:\[code\]public class ObservableListAdatper<T> extends XmlAdapter<LinkedList<T>, ObservableList<T>> {@Overridepublic ObservableList<T> unmarshal(LinkedList<T> v) throws Exception { return FXCollections.observableList(v);}@Overridepublic LinkedList<T> marshal(ObservableList<T> v) throws Exception { LinkedList<T> list = new LinkedList(); list.addAll(v); return list; // Or whatever the correct method is} \[/code\]}Package info:\[code\]@XmlJavaTypeAdapters({@XmlJavaTypeAdapter(value=http://stackoverflow.com/questions/15567044/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;\[/code\]Create Project and save data to xml:\[code\]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();\[/code\]The Xml file after marshall:\[code\]<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ModelDescriptorHelper> <projects/> </ModelDescriptorHelper>\[/code\]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?
 
Back
Top