JAXB List Tag creating inner class

前端 未结 5 1146
无人及你
无人及你 2020-12-16 17:29

So we have an XSD type in the form:


    
        
            

        
5条回答
  •  天命终不由人
    2020-12-16 17:36

    When you define Bars as a complex type, Bars will be generated as separated class. Like this I find schema also easier to read. Bars will not be List in Foo unless you change maxOccurs to a value higher than 1 - you cannot do this on xs:all but you can use xs:sequence.

    ...
        
            
                
            
        
    
        
            
                
            
        
    ...
    

    After running xjc: Foo.java:

        ...
        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "Foo", propOrder = {
    
        })
        public class Foo {
    
            @XmlElement(name = "Bars", required = true)
            protected Bars bars;
    
            public Bars getBars() {
                return bars;
            }
    
            public void setBars(Bars value) {
                this.bars = value;
            }
        }
    

    Bars.java:

        ...
        @XmlAccessorType(XmlAccessType.FIELD)
        @XmlType(name = "Bars", propOrder = {
            "bar"
        })
        public class Bars {
    
            @XmlElement(name = "Bar", required = true)
            protected List bar;
    
            ...
        }
    

    With xs:seqence to get the list of Bars (maxOccurs="unbounded"): XSD:

        ...
        
            
                
            
        
    
        
            
                
            
        
        ...
    

    Foo.java:

    ...
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "Foo", propOrder = {
        "bars"
    })
    public class Foo {
    
        @XmlElement(name = "Bars", required = true)
        protected List bars;
    
        public List getBars() {
            if (bars == null) {
                bars = new ArrayList();
            }
            return this.bars;
        }
    }
    

提交回复
热议问题