2
我想解析對象的XML響應,但它會拋出異常。XML解析返回40個IllegalAnnotationExceptions計數
響應的鏈路是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**