How generate XMLElementWrapper annotation with xjc and customized binding

前端 未结 2 1845
无人共我
无人共我 2020-12-01 05:58

I\'m using JAXB and xjc to compile my XML Schema into Java classes. I do not want to manually edit this generated classes. I have xml schema like that:



        
2条回答
  •  清歌不尽
    2020-12-01 06:22

    First lets break up your schema so that there are no inner classes generated:

    
    
        
    
        
            
                
            
        
    
        
            
                
            
        
    
    

    You'll still get extra classes, just not all in one file. Now you want to add a section to your build to use the jaxb-xew-plugin. I use Maven, so for me this looks like:

    
        org.jvnet.jaxb2.maven2
        maven-jaxb2-plugin
        0.8.2
        
            
                
                    generate
                
                
                    
                        -no-header
                        -Xxew
                        -Xxew:instantiate lazy
                        -Xxew:delete
                    
                    
                        
                            com.github.jaxb-xew-plugin
                            jaxb-xew-plugin
                            1.0
                        
                    
                
            
        
    
    

    If you start using namespaces so that your generated classes have package names, leave off the -Xxew:delete flag, as there's a bug that I recently fixed where it was deleting objects it shouldn't. Alternatively, you could grab the code from github and use it as 1.1-SNAPSHOT.

    When I do that I get the code generated that I think you're looking for:

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "Root", propOrder = {
        "items"
    })
    public class Root {
    
        @XmlElementWrapper(name = "items", required = true)
        @XmlElement(name = "item")
        protected List items;
    
        public List getItems() {
            if (items == null) {
                items = new ArrayList();
            }
            return items;
        }
    
        public void setItems(List items) {
            this.items = items;
        }
    
    }
    

提交回复
热议问题