Search code examples
jaxbjaxb2

JAXB query with respect to including Collection during Unmarshalling Process


I am Using JAXB , and this is my XML Request and the below is the Biding class

<request user="123" Katha="456" />


@XmlRootElement(name = "request")
@XmlAccessorType(XmlAccessType.FIELD)
public class Postdata {

    @XmlAttribute
    private String user;

    @XmlAttribute
    private String Katha;

    // Getters and Setters 

}

The above works fine .

Right now we have change in the Request XML which will be in

<request user="123" Katha="456"> 
    <Specifier name = "One"/>
    <Specifier name = "Two" />
    <Specifier name = "Three" />
</request>

SO please tell me during Unmarshalling process is it possible to include the Specifier as a collection into my Jaxb class ??

@XmlRootElement(name = "request")
@XmlAccessorType(XmlAccessType.FIELD)
public class Postdata {

    @XmlAttribute
    private String user;

    @XmlAttribute
    private String Katha;

    // Getters and Setters 



@XmlElemet
    private List<Specifier> 

Can i do the above way ??

Please let me know ??

}


Solution

  • You can map your use case as follows:

    Postdata

    @XmlRootElement(name = "request")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Postdata {
    
        @XmlAttribute
        private String user;
    
        @XmlAttribute(name="Katha")
        private String Katha;
    
        @XmlElement(name="Specifier")
        private List<Specifier> specifiers;
    
        // Getters and Setters 
    
    }
    

    For More Information