@XMLRootElement versus @XmlType

前端 未结 2 1574
小蘑菇
小蘑菇 2020-12-13 17:59

What\'s the difference between annotating a class with @XMLRootElement and @XMLType. I\'ve been annotating classes with @XMLType when

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 18:32

    The difference between XmlRootElement and XmlType is a matter of scoping. Remember this annotation is merely dictating the creation of the schema used to generate your XML. The XmlRootElement denotes a global element (with an anonymous or schema type):

      <-- schema type
    

    while the XmlType is used to denote a local element (with an anonymous or complex type):

      <-- complex type
    

    The main differences in local/global here are in the hierarchy of the schema your object will appear in and whether you are declaring a schema type or complex type. The documentation for both of these annotations is well written and includes examples:

    XmlRootElement

    XmlType

    EDIT: Addressing the propOrder question: you can use it on a global element if you are also declaring a local type:

    @XmlRootElement (name="PersonElement")
    @XmlType (propOrder={"firstname", "lastname"})
    public class People{
        @XmlElement
        public String firstname;
        public String lastname;
    }
    

    This will yield something like:

    
    
        
            
            
        
    
    

提交回复
热议问题