Android XML 绑定 - Simpleframework
我正在尝试使用 Simpleframework 将 XML 绑定到 POJO,但它无法正常工作,我也不能发现错误。这个结构与 JAXB 完美配合,我只是将注释更改为 Simpleframework 的。也许错误出在 List
的事情上,但我无法弄清楚。
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 + "]";
}
}
I'm trying to bind a XML to POJOs using Simpleframework but it doesn't work correctly and I can't spot the mistake. This structure worked perfectly with JAXB, I just changed the annotations to Simpleframework's. Maybe the mistake is in the List<Track>
thing but I'm not able to figure it out.
Message.toString writes this (header is mapped correctly):
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]]
This is the incomming 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>
And these are the classes I'm using:
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 + "]";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决方案。属性
inline=true
必须位于此处:I found the solution. The attribute
inline=true
must be here: