我試圖JAXB標註添加到類,以解組XML看起來類似於這樣(注意:我不需要名帥一個java豆成XML ...):閱讀在名稱不同,但均是相同類型的元素列表
<fixture_statistics id="3812596">
<home_team_stats id="2">
<id>2</id>
<tackles>58</tackles>
<possession>1868</possession>
<territory>2603</territory>
<minutes_in_22>1316</minutes_in_22>
...
</home_team_stats>
<guest_team_stats id="21061">
<id>21061</id>
<tackles>20</tackles>
<possession>3114</possession>
<territory>2379</territory>
<minutes_in_22>1171</minutes_in_22>
...
</guest_team_stats>
<home_player_1 id="2306143" teamid="2">
<id>2306143</id>
<tackles>3</tackles>
<metres_gained>38</metres_gained>
...
</home_player_1>
<home_player_1 id="2306143" teamid="2">
<id>2306143</id>
<tackles>3</tackles>
<metres_gained>38</metres_gained>
...
</home_player_1>
<home_player_1 id="2306143" teamid="2">
<id>2306143</id>
<tackles>3</tackles>
<metres_gained>38</metres_gained>
...
</home_player_1>
...
<guest_player_1 id="2306143" teamid="2">
<id>2306143</id>
<tackles>3</tackles>
<metres_gained>38</metres_gained>
...
</guest_player_1>
<guest_player_2 id="2306143" teamid="2">
<id>2306143</id>
<tackles>3</tackles>
<metres_gained>38</metres_gained>
...
</guest_player_2>
<guest_player_3 id="2306143" teamid="2">
<id>2306143</id>
<tackles>3</tackles>
<metres_gained>38</metres_gained>
...
</guest_player_3>
...
</fixture_statistics>
*注忽略一些元素值,因爲我複製並粘貼這個問題...
我已成功地映射「fixture_statistics」,「home_team_stats 「和」guest_team_stats「元素到它們各自的類中,並且我能夠正確解組這些元素,但是我遇到了」home_player_n「和」guest_player_n「元素。我創建了包含了這些元素中找到的屬性的類,但我不知道如何處理的事實要素有不同的名稱 - 「home_player_1」高達「home_player_22」與同爲來賓播放器。
這裏是我的燈具統計類的樣本,我的燈具球員的統計類,以便有人能指出我要去的地方錯了...
@XmlRootElement(name = "fixture_statistics")
@XmlAccessorType(XmlAccessType.FIELD)
public class FixtureStatistics {
private Collection<FixturePlayerStatistics> homeTeamPlayerStatistics = new ArrayList<>();
private Collection<FixturePlayerStatistics> guestTeamPlayerStatistics = new ArrayList<>();
}
@XmlAccessorType(XmlAccessType.FIELD)
public class FixturePlayerStatistics {
@XmlElement(name="id")
private Long playerId;
private Integer tackles;
@XmlElement(name="metres_gained")
private Integer metresGained;
}
在FixturePlayerStatistics類我不能添加一個XMlRootElement註釋,因爲該元素可能是44個字符串中的一個,所以我也暫時從FixtureStatistics類的集合中刪除了任何註釋,因爲我確實不確定那裏是什麼。我曾嘗試使用@XmlElementRef,指定所有可能的元素名稱,但那還沒有爲我工作,也無法更改XML,我沒有一個模式來處理,只有從API調用產生的XML。
感謝布萊斯。 出於興趣,我選擇了第一種選擇,因爲我實際上可以使用單獨的對象,以及稍後將它們全部放入集合中。 –