#java #android #xml #binding
#java #Android #xml #привязка
Вопрос:
Я пытаюсь привязать XML к POJOs с помощью Simpleframework, но это работает некорректно, и я не могу обнаружить ошибку. Эта структура отлично работала с JAXB, я просто изменил аннотации на Simpleframework. Возможно, ошибка в List<Track>
этом, но я не могу в этом разобраться.
Message.toString записывает это (заголовок отображается правильно):
Message [body=Body [trackList=TrackList [tracks=[Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null], Track [trackId=null, trackMbid=null, trackLength=null, lyricsId=null, instrumental=null, subtitleId=null, trackName=null, albumName=null, albumId=null, artistId=null, albumCoverart=null, artistMbid=null, artistName=null]]]], header=Header [statusCode=200, executeTime=0.0075960159301758, available=1]]
Это входящий XML:
<message>
<header>
<status_code>200</status_code>
<execute_time>0.0075960159301758</execute_time>
<available>1</available>
</header>
<body>
<track_list>
<track>
<track_id>4110618</track_id>
<track_mbid>888d13a8-5bbb-44d8-b3c9-5c3812020110</track_mbid>
<track_length>0</track_length>
<lyrics_id>6283932</lyrics_id>
<instrumental>0</instrumental>
<subtitle_id>0</subtitle_id>
<track_name>Stan</track_name>
<album_name>How to Be an MC, Volume 13 (Eminem Instrumentals)</album_name>
<album_id>10558107</album_id>
<artist_id>426</artist_id>
<album_coverart_100x100>http://api.musixmatch.com/albumcover/10558107.jpg</album_coverart_100x100>
<artist_mbid>b95ce3ff-3d05-4e87-9e01-c97b66af13d4</artist_mbid>
<artist_name>Eminem</artist_name>
</track>
</track_list>
</body>
И это классы, которые я использую:
package org.lyrics.xml.binding.track;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name = "message")
public class Message {
@Element(name = "header", required = false)
public Header header;
@Element(name = "body", required = false)
public Body body;
public Body getBody() {
return body;
}
public void setBody(Body body) {
this.body = body;
}
public Header getHeader() {
return header;
}
public void setHeader(Header header) {
this.header = header;
}
@Override
public String toString() {
return "Message [body=" body ", header=" header "]";
}
}
package org.lyrics.xml.binding.track;
import org.simpleframework.xml.Element;
public class Header {
@Element(name = "status_code", required = false)
public String statusCode;
@Element(name = "execute_time", required = false)
public String executeTime;
@Element(name = "available", required = false)
public String available;
public String getStatusCode() {
return statusCode;
}
public void setStatusCode(String statusCode) {
this.statusCode = statusCode;
}
public String getExecuteTime() {
return executeTime;
}
public void setExecuteTime(String executeTime) {
this.executeTime = executeTime;
}
public String getAvailable() {
return available;
}
public void setAvailable(String available) {
this.available = available;
}
@Override
public String toString() {
return "Header [statusCode=" statusCode ", executeTime="
executeTime ", available=" available "]";
}
}
package org.lyrics.xml.binding.track;
import org.simpleframework.xml.Element;
public class Body {
@Element(name = "track_list", required = false)
public TrackList trackList;
public TrackList getTrackList() {
return trackList;
}
public void setTrackList(TrackList trackList) {
this.trackList = trackList;
}
@Override
public String toString() {
return "Body [trackList=" trackList "]";
}
}
package org.lyrics.xml.binding.track;
import java.util.List;
import org.simpleframework.xml.ElementList;
public class TrackList {
@ElementList(name = "track", required = false)
public List<Track> tracks;
public List<Track> getTracks() {
return tracks;
}
public void setTracks(List<Track> tracks) {
this.tracks = tracks;
}
@Override
public String toString() {
return "TrackList [tracks=" tracks "]";
}
}
package org.lyrics.xml.binding.track;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
@Root(name = "track")
public class Track {
@Element(name="track_id", required = false)
public String trackId;
@Element(name="track_mbid", required = false)
public String trackMbid;
@Element(name="track_length", required = false)
public String trackLength;
@Element(name="lyrics_id", required = false)
public String lyricsId;
@Element(name="instrumental", required = false)
public String instrumental;
@Element(name="subtitle_id", required = false)
public String subtitleId;
@Element(name="track_name", required = false)
public String trackName;
@Element(name="album_name", required = false)
public String albumName;
@Element(name="album_id", required = false)
public String albumId;
@Element(name="artist_id", required = false)
public String artistId;
@Element(name="album_coverart_100x100", required = false)
public String albumCoverart;
@Element(name="artist_mbid", required = false)
public String artistMbid;
@Element(name="artist_name", required = false)
public String artistName;
public String getTrackId() {
return trackId;
}
public void setTrackId(String trackId) {
this.trackId = trackId;
}
public String getTrackMbid() {
return trackMbid;
}
public void setTrackMbid(String trackMbid) {
this.trackMbid = trackMbid;
}
public String getTrackLength() {
return trackLength;
}
public void setTrackLength(String trackLength) {
this.trackLength = trackLength;
}
public String getLyricsId() {
return lyricsId;
}
public void setLyricsId(String lyricsId) {
this.lyricsId = lyricsId;
}
public String getInstrumental() {
return instrumental;
}
public void setInstrumental(String instrumental) {
this.instrumental = instrumental;
}
public String getSubtitleId() {
return subtitleId;
}
public void setSubtitleId(String subtitleId) {
this.subtitleId = subtitleId;
}
public String getTrackName() {
return trackName;
}
public void setTrackName(String trackName) {
this.trackName = trackName;
}
public String getAlbumName() {
return albumName;
}
public void setAlbumName(String albumName) {
this.albumName = albumName;
}
public String getAlbumId() {
return albumId;
}
public void setAlbumId(String albumId) {
this.albumId = albumId;
}
public String getArtistId() {
return artistId;
}
public void setArtistId(String artistId) {
this.artistId = artistId;
}
public String getAlbumCoverart() {
return albumCoverart;
}
public void setAlbumCoverart(String albumCoverart) {
this.albumCoverart = albumCoverart;
}
public String getArtistMbid() {
return artistMbid;
}
public void setArtistMbid(String artistMbid) {
this.artistMbid = artistMbid;
}
public String getArtistName() {
return artistName;
}
public void setArtistName(String artistName) {
this.artistName = artistName;
}
@Override
public String toString() {
return "Track [trackId=" trackId ", trackMbid=" trackMbid
", trackLength=" trackLength ", lyricsId=" lyricsId
", instrumental=" instrumental ", subtitleId="
subtitleId ", trackName=" trackName ", albumName="
albumName ", albumId=" albumId ", artistId=" artistId
", albumCoverart=" albumCoverart ", artistMbid="
artistMbid ", artistName=" artistName "]";
}
}
Ответ №1:
Я нашел решение. Здесь должен быть атрибут inline=true
:
@ElementList(name = "track", required = false)
public List<Track> tracks;