Handling nested elements in JAXB

前端 未结 4 807
梦毁少年i
梦毁少年i 2021-02-05 12:48

I am wondering if it is possible to have JAXB not to create Java object for XML elements that serve as wrappers. For example, for XML of the following structure

         


        
4条回答
  •  醉酒成梦
    2021-02-05 13:20

    Although it requires extra coding, the desired unmarshalling is accomplished in the following way using a transient wrapper object:

    @XmlRootElement(name = "root")
    public class Root {
    
        private Entity entity;
    
        static class Entity {
    
        }
    
        static class EntityWrapper {
            @XmlElement(name = "entity")
            private Entity entity;
    
            public Entity getEntity() {
                return entity;
            }
        }
    
        @XmlElement(name = "wrapper")
        private void setEntity(EntityWrapper entityWrapper) {
            entity = entityWrapper.getEntity();
        }
    
    }
    

提交回复
热议问题