马歇尔列表到XML工作 - 但如何解组? [英] Marshall a List to XML works - but how to unmarshall?

查看:101
本文介绍了马歇尔列表到XML工作 - 但如何解组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I can marshall a ObservableList using a "Wrapper"-class like below. But I cannot unmarshall it back to the wrapperclass it was before.

这个想法是:
我有一个ObtenableListExpenses。我将此List放入包装类并将此类保存为XML。结果如下所示:

The idea is: I have an ObservableList of "Expenses". I put this List into a wrapper-class and save this class to XML. The result looks like this:

<?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>

我无法将其带回包装对象。
我非常感谢任何帮助。

I cannot bring it back to the wrapper-object. I really appreciate any kind of help.

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

Main-class-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();
        }
    }
}

Wrapper-class :

公共类MyWrapperForList {

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)
公共类费用{

@XmlRootElement(name = "root") public class Expense {

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

I used this tutorial from Blaise Doughan: http://blog.bdoughan.com/2012/11/creating-generic-list-wrapper-in-jaxb.html

推荐答案

MyListWrapper



如果你想要 MyWrapperForList 来解组持有 ObservableList <的实例/ code>然后您需要通过以下方式之一设置您的课程。

MyListWrapper

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.

类型的属性 ObservableList

Property of Type 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;
    }

}

列表属性初始化为 ObservableList的实例

List Property Initialized to Instance of 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。列表中有
的确切数量的对象,但所有属性都引用
with null。 :(

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. :(

我认为我在 ObservableList 方面的隧道视野仅限于想念您真正的问题是如何映射费用类。由于您只有获取方法,您应该映射到字段使用 @XmlAccessorType(XmlAccessType.FIELD),如下所示。

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;
    }

}

这篇关于马歇尔列表到XML工作 - 但如何解组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆