在JAXB中编组抽象类的arraylist [英] Marshalling arraylist of abstract class in JAXB

查看:71
本文介绍了在JAXB中编组抽象类的arraylist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序,我想用XML而不是数据库保存数据。

I have a Java application that I want to save the data in XML instead of a database.

我们决定使用JAXB而不是生成基于的文件在架构中,我们只是在Java文件中添加了注释。

We decided to go with JAXB and instead of generating files based on the schema, we just added annotations to our Java files.

我们遇到的问题是我们有一个名为Node的抽象类的ArrayList。 Node具有Module或ScreenImage的子类。当我们编组arraylist时,它不会保存类型。例如:

The issue we are running into is that we have an ArrayList of an abstract class called Node. A Node has subclasses of either Module or ScreenImage. When we marshall the arraylist, it doesn't save the type. Such as:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Module"

JAXB是否支持抽象元素?

Does JAXB support elements that are abstract?

如何让JAXB保存类型,以便我可以解组它?

How do I get JAXB to save the type, so that I'm able to unmarshall it?

推荐答案

应该可以正常工作。
请注意,如果您的模块 ScreenImage 不是静态可从绑定类(即 JAXBContext.newInstance(...)中指定的类),它们应该显式绑定(将它们添加到 JAXBContext.newInstance (...)参数)。

It should work fine. Note that if your Module and ScreenImage are not statically accessible from the bound classes (i.e. the classes specified in JAXBContext.newInstance(...)), they should be bound explicitly (add them to the JAXBContext.newInstance(...) parameters).

这篇关于在JAXB中编组抽象类的arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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