2017-08-25 211 views
0

我一直在試圖將自我創建的xml配置文件解組爲一個JAXB默認實現的對象,我忽略了原因,但是一些內部元素和值被略過給我空結果。JAXB忽略內部元素

這裏是XML文檔:

<import-sources> 
    <domain-objects> 
     <domain-object class="xxx.xxx.core.business.mapping.Make"> 
      <sources> 
       <source name="xxxxxx" class="xxx.xxx.core.web.common.model.xxxxxxx.output.MakeDTO"> 
        <uri secured="true" value="xxxxxxxxxxxxxxxxxx"/> 
        <key name="api_key" mode="in_uri" value="xxxxxxxxxxxx"/> 
       </source> 
      </sources> 
    </domain-object> 
</domain-objects> 

這是映射類:

@XmlRootElement(name = "import-sources") 
public class ImportSources { 

    @XmlType 
    public static class DomainObject { 

     @XmlType 
     public static class Source { 

      @XmlType 
      public static class URI { 

       private String value; 

       private boolean secured; 
      } 

      @XmlType 
      public static class Key { 

       @XmlEnum 
       public enum Mode { 
        @XmlEnumValue("in_uri") 
        IN_URI, 
        @XmlEnumValue("in_header") 
        IN_HEADER 
       } 

       private String name; 

       private String value; 

       private Mode mode; 
      } 


      private String name; 


      private URI uri; 


      private Key key; 

      @XmlElement(name = "class") 
      private Class outputClass; 
     } 

     @XmlAttribute(name = "class") 
     private Class<? extends Entity> subjectClass; 

     @XmlElementWrapper 
     @XmlElement(name = "source") 
     private Source[] sources; 
    } 

    @XmlElementWrapper(name = "domain-objects") 
    @XmlElement(name = "domain-object") 
    private DomainObject[] domainObjects; 
} 

解析與Unmarshaller.unmarshall文檔()所有源的字段爲空後,如果我替換它的內部標籤屬性,我可以得到它的標量字段:

<source> 
    <name>....</name> 
    <class>...</class> 
</source> 

我想避免這種冗餘。

回答

0

我終於通過使用'@XmlAttribute'註解打開標籤或單個標籤內的所有字段來解決我的問題。

0

我建議相應地明確註釋字段爲@XmlElement@XmlAttribute。然後你的班級將如下所示,並且字段將填充:

@XmlRootElement(name = "import-sources") 
public class ImportSources { 

    @XmlType 
    public static class DomainObject { 

     @XmlType 
     public static class Source { 

      @XmlType 
      public static class URI { 


       @XmlAttribute //here 
       private String value; 

       @XmlAttribute //here 
       private boolean secured; 
      } 

      @XmlType 
      public static class Key { 

       @XmlEnum 
       public enum Mode { 
        @XmlEnumValue("in_uri") 
        IN_URI, 
        @XmlEnumValue("in_header") 
        IN_HEADER 
       } 

       @XmlAttribute //here 
       private String name; 

       @XmlAttribute //here 
       private String value; 

       @XmlAttribute //here 
       private Mode mode; 
      } 


      @XmlAttribute //here 
      private String name; 

      @XmlElement //here 
      private URI uri; 

      @XmlElement //here 
      private Key key; 

      @XmlElement(name = "class") 
      private Class outputClass; 
     } 

     @XmlAttribute(name = "class") 
     private Class<? extends Entity> subjectClass; 

     @XmlElementWrapper 
     @XmlElement(name = "source") 
     private Source[] sources; 
    } 
}