在 JAXB 编组期间是否可以忽略包装类

2022-01-19 00:00:00 xml java 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>

我想要实现的目标如下:

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.

相关文章