java编组是什么意思_java-将列表编组为XML可以工作-但是如何解组?

我可以使用“包装器”类来编组ObservableList,如下所示.但是我无法将其解组回以前的wrapper类.

这个想法是:

我有一个“费用”的ObservableList.我将此列表放入包装器类中,并将该类保存为XML.结果看起来像这样:

[none]

Year

asd

354

我无法将其带回包装对象.

我真的很感谢任何帮助.

主类JAXBContext(对所有人可见):

JAXBContext jc = JAXBContext.newInstance(MyWrapperForList.class, Expense.class);

主类SAVEBUTTON:

public class SaveButtonListener implements EventHandler {

@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 saveList = new ArrayList<>();

saveList.addAll(data);

MyWrapperForList wrapper = new MyWrapperForList<>(saveList);

JAXBElement jaxbElement = new JAXBElement<>(

new QName("List"), MyWrapperForList.class, wrapper);

m.marshal(jaxbElement, xmlOut);

xmlOut.flush();

xmlOut.close();

主类LOADBUTTON:

public class LoadButtonListener implements EventHandler {

@Override

public void handle(ActionEvent arg0) {

try {

Unmarshaller unmarshaller = jc.createUnmarshaller();

StreamSource xml = new StreamSource(PATH);

MyWrapperForList unwrapper = unmarshaller.unmarshal(xml,

MyWrapperForList.class).getValue();

List tempList = new ArrayList<>();

tempList.addAll(unwrapper.getItems());

System.out.println(tempList.get(0).getTitle());

} catch (Exception e) {

e.printStackTrace();

}

}

}

包装类:

公共类MyWrapperForList {

private List list;

public MyWrapperForList() {

list = new ArrayList<>();

}

public MyWrapperForList(List expenses) {

this.list = expenses;

}

@XmlAnyElement(lax=true)

public List 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;

}

}

解决方法:

MyListWrapper

如果希望MyWrapperForList解组持有ObservableList实例的实例,则需要通过以下方式之一设置类.

类型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属性初始化为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;

}

}

示范代码

输入(nub.xml)

[none]

Year

dfg

4

[none]

Year

ROBO

1234

演示

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

}

}

}

产量

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.

标签:java,xml,jaxb,marshalling

来源: https://codeday.me/bug/20191013/1905968.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值