是否可以在JAXB编组期间忽略包装类 [英] Is it possible to ignore a wrapper class during JAXB marshalling

查看:103
本文介绍了是否可以在JAXB编组期间忽略包装类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让JAXB在编组过程中忽略包装类,在代码中使用这个包装类是有意义的,因为它将所有相关信息保存在一起,但是我需要在编组期间摆脱它处理。以下是相关代码。

I'm trying to get JAXB to ignore a wrapper class during the Mashalling process, it makes sense to have this wrapper class in code, as it keep all related information together, however I need to get rid of it during the marshaling process. The following is the relevant code.

@XmlType(name = "root")
@XmlRootElement(name = "root")
public class Root {

    @XmlElementRef
    private List<Resource> resources = new ArrayList<>();

    public void addResource(Resource resource) {
        resources.add(resource);
    }
}


@XmlRootElement(name = "", namespace = "")
@XmlAccessorType(XmlAccessType.NONE)
public class Resource {

    @XmlElementRef
    private Element element;
    @XmlElementRef
    private FieldType fieldType;
    @XmlElementRef
    private ListType listType;
}

Root是主要对象,Resource是我的包装器对象喜欢没有为其创建节点。我仍然希望渲染资源中的Element,FieldType和ListType。

Root is the main object, and Resource is the wrapper object that I'd like not have a node created for. I still want the Element, FieldType and ListType within the Resource to be rendered however.

这就是我目前所拥有的:

This is what I currently have:

<root>
    <>
        <element name="resource1"/>
        <fieldType name="resource1--type">
        </fieldType>
        <listType name="resource--list">
        </listType>
    </>
    <>
        <element name="resource2"/>
        <fieldType name="resource2--type">
        </fieldType>
        <listType name="resource2--list">
        </listType>
    </>
</root>

我想要实现的目标如下:

What I'd like to achieve is the following:

<root>
    <element name="resource1"/>
    <fieldType name="resource1--type">
    </fieldType>
    <listType name="resource--list">
    </listType>
    <element name="resource2"/>
    <fieldType name="resource2--type">
    </fieldType>
    <listType name="resource2--list">
    </listType>
</root>

我不知道是否有可能,但任何帮助都将不胜感激。

I don't know if it's possible, but any help would be appreciated.

谢谢。

推荐答案

你无法在JAXB中实现这一点。即使您能够像这样序列化,例如使用XmlAdapter,也无法对其进行反序列化。

You cannot achieve that in JAXB. Even if you would be able to serialize like this, using a XmlAdapter for example, it will be impossible to deserialize it.

试试这个:

@XmlType(name = "root")
@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.NONE)
public class Root {

    private ArrayList<Resource> resources = new ArrayList<Resource>();

    public void addResource(Resource resource) {
        resources.add(resource);
    }

    @XmlElementRefs(value = { @XmlElementRef(type = Element.class),
                              @XmlElementRef(type = ListType.class),
                              @XmlElementRef(type = FieldType.class) })
    public List<Object> getResourceFields() {
        List<Object> list = new ArrayList<Object>();
        for (Resource r : resources) {
            list.add(r.getElement());
            list.add(r.getFieldType());
            list.add(r.getListType());
        }
        return list;
    }
}

基本上 getRerourceFields 连接同一列表中的所有资源字段。
如果您无法更改 Root 类,这可能是您的 RootAdapter 并将其用作@Biju建议。

Basically getRerourceFields concatenates all the resources' fields in the same list. If you cannot change the Root class, this could be your RootAdapter and use it as @Biju suggested.

这篇关于是否可以在JAXB编组期间忽略包装类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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