XML解析返回40个IllegalAnnotationExceptions计数

时间:2014-03-25 11:41:27

标签: java jaxb

我正在尝试解析对象的XML响应,但它会抛出异常。

回复链接为this

<response>
  <meta>
     <per_page>10</per_page>
     <total>20</total>
     <geolocation>None</geolocation>
     <took>8</took>
     <page>1</page>
  </meta>
  <events>
  <event>
   ...
  </event>
  <event>
   ...
  </event> 
  ....
  </events>
</response>

代码

     queryString = queryString.replaceAll(" ", "%20");
    try {
        URL page = new URL(queryString);

        HttpURLConnection conn = (HttpURLConnection) page.openConnection();
        conn.connect();
        InputStreamReader in = new InputStreamReader(conn.getInputStream(),Charset.forName("UTF-8"));

       this.response = (Response) JAXB.unmarshal(in, Response.class);
        } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    } 

异常

javax.xml.bind.DataBindingException: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 40   
counts of IllegalAnnotationExceptions
Class has two properties of the same name "events"
    this problem is related to the following location:
....

对象类

@XmlRootElement(name = "Response")
public class Response {
    @XmlElement(name="per_page")
    private int per_page;
    @XmlElement(name="total")
    private int total;
    @XmlElement(name="geolocation")
    private String geolocation;
    @XmlElement(name="took")
    private int took;
    @XmlElement(name="page")
    private int page;
    @XmlElement(name="events")
    private List<Event> events = null;

    **getters and setters**

物件

@XmlRootElement(name="event")
public class Event {
    @XmlElement(name = "links")
    private String link;
    @XmlElement(name = "id")
    private int id;
    @XmlElement(name = "stats")
    private Stats stats;
    @XmlElement(name = "title")
    private String title;
    @XmlElement(name = "announce_date")
    private String announce_date;
    @XmlElement(name = "score")
    private float score;
    @XmlElement(name = "date_tbd")
    private boolean date_tbd;
    @XmlElement(name = "type")
    private String type;
    @XmlElement(name = "datetime_local")
    private String datetime_local;
    @XmlElement(name = "visible_until_utc")
    private String visible_util_utc;
    @XmlElement(name = "time_tbd")
    private boolean time_tbd;
    @XmlElement(name = "taxonomies")
    private List<Taxonomie> taxonomies;
    @XmlElement(name = "performers")
    private List<Performer> performers;
    @XmlElement(name = "url")
    private String url;
    @XmlElement(name = "created_at")
    private String created_at;
    @XmlElement(name = "venue")
    private Venue venue;
    @XmlElement(name = "short_title")
    private String short_title;
    @XmlElement(name = "datetime_utc")
    private String datetime_utc;
    @XmlElement(name = "datetime_tbd")
    private boolean datetime_tbd;

    **getters and setters**

1 个答案:

答案 0 :(得分:1)

默认情况下,JAXB实现将公共字段和属性视为映射。当您注释非公共字段时,它也会被映射。然后,如果您有一个映射字段,具有相同名称的属性,您将获得此异常。

当您注释字段时,需要使用@XmlAccessorType(XmlAccessType.FIELD)为您的班级添加注释。


注意:

您目前在模型上添加的注释多于您需要的注释。由于JAXB是异常配置,因此您只需要在希望XML表示与默认值不同的地方添加注释。