简体   繁体   English

JAXB:使用同一元素的多个名称解组xml

[英]JAXB: unmarshalling xml with multiple names for the same element

I figure this will be easy for someone who really understands JAXB binding files... 我认为这对于真正理解JAXB绑定文件的人来说很容易......

Basic Question 基本问题

How do you configure JAXB to unmarshal multiple elements into the same class? 如何配置JAXB以将多个元素解组到同一个类中?

Note: I want to avoid adding another dependency to my project (like MOXy). 注意:我想避免为我的项目添加另一个依赖项(如MOXy)。 Ideally, this can be accomplished with annotations or a custom bindings file. 理想情况下,这可以通过注释或自定义绑定文件来完成。

Background 背景

I have an XML document that contains lots of variations of the same element--each with the exact same properties. 我有一个XML文档,其中包含相同元素的许多变体 - 每个变体具有完全相同的属性。 Using my example below, all I care about is "Employees" but the XML specifies "directors, managers and staff." 使用下面的示例,我关心的只是“员工”,但XML指定“董事,经理和员工”。 For our purposes, these are all subclasses of the same parent and we only need to work with the parent type (Employee) and our object model doesn't have or need instances of the subclasses. 出于我们的目的,这些都是同一父类的子类,我们只需要使用父类型(Employee),并且我们的对象模型没有或不需要子类的实例。

I want JAXB to bind any instance of director, manager, or staff elements into an Employee object. 我希望JAXB将任何director, manager, or staff元素的实例绑定到Employee对象中。

Example

input: 输入:

<organization>
    <director>
        <fname>Dan</fname>
        <lname>Schman</lname>
    </director>    
    <manager>
        <fname>Joe</fname>
        <lname>Schmo</lname>
    </manager>    
    <staff>
        <fname>Ron</fname>
        <lname>Schwan</lname>
    </staff>    
    <staff>
        <fname>Jim</fname>
        <lname>Schwim</lname>
    </staff>    
    <staff>
        <fname>Jon</fname>
        <lname>Schwon</lname>
    </staff>    
</organization>

output: 输出:

After unmarshalling this example, I would end up with an Organization object with one property: List<Employees> employees where each employee only has a firstName and lastName. 在解组此示例之后,我最终得到一个具有一个属性的Organization对象: List<Employees> employees ,其中每个雇员只有firstName和lastName。

(Note: each employee would be of type Employee NOT Director/Manager/Staff . Subclass information would be lost when unmarshalling. We also don't care about marshaling back out--we only need to create objects from XML) (注意:每个员工都是Employee NOT Director/Manager/Staff 。在解组时子类信息会丢失。我们也不关心编组 - 我们只需要从XML创建对象)

Can this be done without extensions like MOXy? 这可以在没有MOXy这样的扩展的情况下完成吗? Can a custom bindings.xjb file save the day? 自定义bindings.xjb文件可以保存一天吗?

This corresponds to a choice structure. 这对应于选择结构。 You could use an @XmlElements annotation for this use case: 您可以为此用例使用@XmlElements批注:

@XmlElements({
    @XmlElement(name="director", type=Employee.class),
    @XmlElement(name="manager", type=Employee.class)
})
List<Employee> getEmployees() {
    return employees;
}

If you are starting from an XML schema the following will help: 如果您从XML模式开始,以下内容将有所帮助:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM