Dealing with JAXB Collections

前端 未结 2 711
悲哀的现实
悲哀的现实 2021-01-02 10:07

I am trying to unmarshall the following XML using JAXB:


    
        
            
                        


        
相关标签:
2条回答
  • 2021-01-02 10:18

    The @XmlElementWrapper plugin does exactly what you want.

    0 讨论(0)
  • 2021-01-02 10:20

    For anybody who can not or does not want to use the plugin: If you can live with a different XML structure, it's possible to avoid generating the extra wrapper classes by simply using maxoccurs="unbounded" and leaving out the containing element. Using the original example:

    <xsd:element name="Work" type="Work" maxOccurs="unbounded" minOccurs="0"/>
    
    <xsd:complexType name="Work">
        <xsd:sequence>
            <xsd:element name="Composer" type="Composer" maxOccurs="unbounded" minOccurs="0"/>
        </xsd:sequence>
    </xsd:complexType>
    
    <xsd:complexType name="Composer">
        <xsd:sequence>
            <xsd:element name="Name" type="xsd:string"></xsd:element>
        </xsd:sequence>
    </xsd:complexType>
    

    will produce a structure like this:

    <Work>
        <Composer>
            <Name>A name</Name>
        </Composer>
        <Composer>
            <Name>A name 2</Name>
        </Composer>
    </Work>
    

    This will put a method on the Work type that returns a List<Composer> object. Unfortunately the method is called getComposer instead of getComposers, but you can use annotations or custom bindings to fix that problem.

    0 讨论(0)
提交回复
热议问题