1

我可以使用如下所示的“Wrapper”类来编组一个 ObservableList。但我无法将它解组回之前的包装类。

这个想法是:我有一个“费用”的 ObservableList。我把这个 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);

主类 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();
        }
    }
}

包装类:

公共类 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

4

1 回答 1

1

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.ObservableListWrapper
forum18594548.Expense@789df61d
forum18594548.Expense@4a8927c8

更新

第一:感谢您为 Blaise 工作!!我真的很高兴你对我做了什么!我尝试了您在此处编写的内容(几乎与我的相同),并且得到了与您相似(相同类型)的输出。但是列表中的对象都用 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;
    }

}
于 2013-09-05T14:59:37.453 回答