MyListWrapper
如果要
MyWrapperForList解组持有的实例,
ObservableList则需要通过以下方式之一设置类。
类型属性ObservableList
import javax.xml.bind.annotation.XmlAnyElement;import javafx.collections.*;public class MyWrapperForList<T> { private ObservableList<T> list; public MyWrapperForList() { list = FXCollections.<T>observableArrayList(); } public MyWrapperForList(ObservableList<T> list) { this.list = list; } @XmlAnyElement(lax = true) public ObservableList<T> getItems() { return list; }}List
属性已初始化为的实例 ObservableList
import java.util.List;import javax.xml.bind.annotation.XmlAnyElement;import javafx.collections.*;public class MyWrapperForList<T> { private List<T> list = FXCollections.<T>observableArrayList(); public MyWrapperForList() { list = FXCollections.<T>observableArrayList(); } public MyWrapperForList(List<T> list) { this.list = list; } @XmlAnyElement(lax = true) public List<T> getItems() { return list; }}示范代码
输入(nub.xml)
<List> <root> <category>[none]</category> <period>Year</period> <title>dfg</title> <value>4</value> </root> <root> <category>[none]</category> <period>Year</period> <title>ROBO</title> <value>1234</value> </root></List>
演示版
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<Expense> wrapper = (MyWrapperForList<Expense>) unmarshaller.unmarshal(xml, MyWrapperForList.class).getValue(); List<Expense> data = wrapper.getItems(); System.out.println(data.getClass()); for(Expense expense : data) { System.out.println(expense); } }}输出量
class com.sun.javafx.collections.ObservableListWrapperforum18594548.Expense@789df61dforum18594548.Expense@4a8927c8
更新
第一:感谢您的工作,布莱斯!我为您对我所做的一切感到非常高兴!我尝试了您在这里写的内容(与我写的差不多),并且得到了与您类似的输出(相同类型)。但是列表中的对象全部用null引用。如果我写System.out.println(data.get(0).getTitle());
它说空。列表中有确切数量的对象,但是所有属性均引用为null。:(
我想我在这
ObservableList方面有了远见,只是想念您真正的问题是您如何映射
Expense课程。由于只有
get方法,因此应使用
@XmlAccessorType(XmlAccessType.FIELD)以下方法映射到字段。
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; }}


