I can marshall a ObservableList using a \"Wrapper\"-class like below. But I cannot unmarshall it back to the wrapperclass it was before.
The idea is: I have an Observabl
If you want MyWrapperForList
to unmarshal holding an instance of ObservableList
then you will need to setup your class in one of the following ways.
Property of Type ObservableList
import javax.xml.bind.annotation.XmlAnyElement;
import javafx.collections.*;
public class MyWrapperForList {
private ObservableList list;
public MyWrapperForList() {
list = FXCollections.observableArrayList();
}
public MyWrapperForList(ObservableList list) {
this.list = list;
}
@XmlAnyElement(lax = true)
public ObservableList getItems() {
return list;
}
}
List
Property Initialized to Instance of ObservableList
import java.util.List;
import javax.xml.bind.annotation.XmlAnyElement;
import javafx.collections.*;
public class MyWrapperForList {
private List list = FXCollections.observableArrayList();
public MyWrapperForList() {
list = FXCollections.observableArrayList();
}
public MyWrapperForList(List list) {
this.list = list;
}
@XmlAnyElement(lax = true)
public List getItems() {
return list;
}
}
Input (nub.xml)
[none]
Year
dfg
4
[none]
Year
ROBO
1234
Demo
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(MyWrapperForList.class, Expense.class);
//UNMARSHALLING
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource xml = new StreamSource("src/forum18594548/nub.xml");
MyWrapperForList wrapper = (MyWrapperForList) unmarshaller.unmarshal(xml, MyWrapperForList.class).getValue();
List data = wrapper.getItems();
System.out.println(data.getClass());
for(Expense expense : data) {
System.out.println(expense);
}
}
}
Output
class com.sun.javafx.collections.ObservableListWrapper
forum18594548.Expense@789df61d
forum18594548.Expense@4a8927c8
UPDATE
First: Thanks for you work Blaise!! I'm really glad for what you do to me! I tried what you wrote here (it was nearly the same as I had) and I got a similar (same type of) output as you got. BUT the objects in the lists are all referenced with null. If I write System.out.println(data.get(0).getTitle()); it says null. There is the exact amount of objects in the list, but all attributes are referenced with null. :(
I think I got tunnel vision on the ObservableList
aspect only to miss your real problem was with how you mapped the Expense
class. Since you only have get
methods you should map to the fields using @XmlAccessorType(XmlAccessType.FIELD)
as follows.
import javax.xml.bind.annotation.*;
@XmlRootElement(name="root")
@XmlAccessorType(XmlAccessType.FIELD)
public class Expense {
private String title;
private String category;
private String period;
private String value;
public Expense() {
}
public Expense(String title, String value, String period, String category) {
this.title = title;
this.value = value;
this.period = period;
this.category = category;
}
public String getTitle() {
return this.title;
}
public String getCategory() {
return this.category;
}
public String getPeriod() {
return this.period;
}
public String getValue() {
return this.value;
}
}