我可以使用如下所示的“Wrapper”类来编组一个ObservableList。但是我不能把它拆分回以前的包装类。
这个想法是: 我有一个ObtenableList“Expenses”。我将此List放入包装类中并将此类保存为XML。结果如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<List>
<root>
<category>[none]</category>
<period>Year</period>
<title>asd</title>
<value>354</value>
</root>
</List>
我无法将其带回包装对象。 我真的很感激任何帮助。
主类JAXBContext(对所有人都可见):
JAXBContext jc = JAXBContext.newInstance(MyWrapperForList.class, Expense.class);
Main-class SAVEBUTTON:
public class SaveButtonListener implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent arg0) {
File serializedFile = new File(PATH);
try {
if (serializedFile.exists() == false)
serializedFile.createNewFile();
PrintWriter xmlOut = new PrintWriter(serializedFile);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
List<Expense> saveList = new ArrayList<>();
saveList.addAll(data);
MyWrapperForList<Expense> wrapper = new MyWrapperForList<>(saveList);
JAXBElement<MyWrapperForList> jaxbElement = new JAXBElement<>(
new QName("List"), MyWrapperForList.class, wrapper);
m.marshal(jaxbElement, xmlOut);
xmlOut.flush();
xmlOut.close();
主要级-LOADBUTTON:
public class LoadButtonListener implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent arg0) {
try {
Unmarshaller unmarshaller = jc.createUnmarshaller();
StreamSource xml = new StreamSource(PATH);
MyWrapperForList<Expense> unwrapper = unmarshaller.unmarshal(xml,
MyWrapperForList.class).getValue();
List<Expense> tempList = new ArrayList<>();
tempList.addAll(unwrapper.getItems());
System.out.println(tempList.get(0).getTitle());
} catch (Exception e) {
e.printStackTrace();
}
}
}
包装级:
public class MyWrapperForList {
private List<Expense> list;
public MyWrapperForList() {
list = new ArrayList<>();
}
public MyWrapperForList(List<Expense> expenses) {
this.list = expenses;
}
@XmlAnyElement(lax=true)
public List<Expense> getItems() {
return list;
}
}
费用级:
@XmlRootElement(name =“root”) 公共类费用{
private String title;
private String category;
private String period;
private String value;
public Expense() {} //Default constructor is needed for XML-handling
public Expense(String title, String value, String period, String category) {
this.title = title;
this.value = value;
this.period = period;
this.category = category;
}
@XmlElement(name = "title")
public String getTitle() {
return this.title;
}
@XmlElement(name = "category")
public String getCategory() {
return this.category;
}
@XmlElement(name = "period")
public String getPeriod() {
return this.period;
}
@XmlElement(name = "value")
public String getValue() {
return this.value;
}
}
我使用了Blaise Doughan的这个教程:http://blog.bdoughan.com/2012/11/creating-generic-list-wrapper-in-jaxb.html
答案 0 :(得分:1)
如果您希望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.ObservableListWrapper
forum18594548.Expense@789df61d
forum18594548.Expense@4a8927c8
<强>更新强>
第一:谢谢你的工作Blaise !!我很高兴你做了什么 我!我试过你在这里写的东西(它和我的差不多)和 我得到了类似(相同类型)的输出。但是对象在 这些列表都以null引用。如果我写 的System.out.println(data.get(0).getTitle());它说null。有 列表中确切的对象数量,但引用了所有属性 用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;
}
}