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

查看:23
本文介绍了是否可以在 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 是我不希望为其创建节点的包装器对象.但是,我仍然希望呈现 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.

这是我目前拥有的:

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

我想达到的目标如下:

<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天全站免登陆