使用 SimpleFramework 反序列化此 XML 数组
Deserializing this XML Array with SimpleFramework
我正在尝试用 SimpleFramework
反序列化此 XML 数组,并且我将 运行 保留在这个问题中:
07-04 16:14:59.840 1681-1681/com.sampleapp D/SampleApp﹕ org.simpleframework.xml.core.ElementException: Element 'image' does not have a match in class com.todocodepathapp.api.models.Images at line 5
这是从 API 返回的 xml 数组:
<response>
<data>
<images>
<image>
<url>http://24.media.tumblr.com/tumblr_lmdpfs0UcO1qbe5pxo1_1280.jpg</url>
<id>9ik</id>
<source_url>http://thecatapi.com/?id=9ik</source_url>
</image>
<image>
<url>http://25.media.tumblr.com/tumblr_m6nl30dMm21qz59j6o1_1280.jpg</url>
<id>MTU4NTM4NA</id>
<source_url>http://thecatapi.com/?id=MTU4NTM4NA</source_url>
</image>
<image>
<url>http://25.media.tumblr.com/tumblr_m1e6655aqv1qz85pko1_500.jpg</url>
<id>avd</id>
<source_url>http://thecatapi.com/?id=avd</source_url>
</image>
<image>
<url>http://24.media.tumblr.com/tumblr_lz7cx55fOS1qbd47zo1_1280.jpg</url>
<id>cu8</id>
<source_url>http://thecatapi.com/?id=cu8</source_url>
</image>
</images>
</data>
</response>
这些是我正在使用的模型 类(我有一种预感,我不应该使用 Images.java,应该只有是三个 类):
Response.java
@Root(name = "response")
public class Response {
@Element(name = "data")
private Data mData;
public Data getData() {
return mData;
}
}
Data.java
@Root(name = "data")
public class Data {
@Element(name = "images")
private Images mImages;
public Images getImages() {
return mImages;
}
}
Images.java
@Root(name = "images")
public class Images {
@ElementList(entry = "image")
private List<Image> mImageList;
public List<Image> getImageList() {
return mImageList;
}
}
Image.java
@Root(name = "image")
public class Image {
@Element(name = "url")
private String mUrl;
public String getUrl() {
return mUrl;
}
@Element(name = "id")
private String mId;
public String getId() {
return mId;
}
@Element(name = "source_url")
public String mSourceUrl;
public String getSourceUrl() {
return mSourceUrl;
}
}
我应该如何完成这个?我认为添加带有条目 @ElementList(entry = "image")
的注释可以解决这个问题?
我的预感是正确的,我继续删除了 class Images.java
并在我的 Data.java
模型 class 中添加了以下注释:
@ElementList(name = "images", entry = "image")
private List<Image> mImages;
public List<Image> getImages() {
return mImages;
}
请注意,我将图像模型 @Root
更改为 @Element
class:
@Element(name = "image")
public class Image {
@Element(name = "url")
private String mUrl;
public String getUrl() {
return mUrl;
}
@Element(name = "id")
private String mId;
public String getId() {
return mId;
}
@Element(name = "source_url")
public String mSourceUrl;
public String getSourceUrl() {
return mSourceUrl;
}
}
我正在尝试用 SimpleFramework
反序列化此 XML 数组,并且我将 运行 保留在这个问题中:
07-04 16:14:59.840 1681-1681/com.sampleapp D/SampleApp﹕ org.simpleframework.xml.core.ElementException: Element 'image' does not have a match in class com.todocodepathapp.api.models.Images at line 5
这是从 API 返回的 xml 数组:
<response>
<data>
<images>
<image>
<url>http://24.media.tumblr.com/tumblr_lmdpfs0UcO1qbe5pxo1_1280.jpg</url>
<id>9ik</id>
<source_url>http://thecatapi.com/?id=9ik</source_url>
</image>
<image>
<url>http://25.media.tumblr.com/tumblr_m6nl30dMm21qz59j6o1_1280.jpg</url>
<id>MTU4NTM4NA</id>
<source_url>http://thecatapi.com/?id=MTU4NTM4NA</source_url>
</image>
<image>
<url>http://25.media.tumblr.com/tumblr_m1e6655aqv1qz85pko1_500.jpg</url>
<id>avd</id>
<source_url>http://thecatapi.com/?id=avd</source_url>
</image>
<image>
<url>http://24.media.tumblr.com/tumblr_lz7cx55fOS1qbd47zo1_1280.jpg</url>
<id>cu8</id>
<source_url>http://thecatapi.com/?id=cu8</source_url>
</image>
</images>
</data>
</response>
这些是我正在使用的模型 类(我有一种预感,我不应该使用 Images.java,应该只有是三个 类):
Response.java
@Root(name = "response")
public class Response {
@Element(name = "data")
private Data mData;
public Data getData() {
return mData;
}
}
Data.java
@Root(name = "data")
public class Data {
@Element(name = "images")
private Images mImages;
public Images getImages() {
return mImages;
}
}
Images.java
@Root(name = "images")
public class Images {
@ElementList(entry = "image")
private List<Image> mImageList;
public List<Image> getImageList() {
return mImageList;
}
}
Image.java
@Root(name = "image")
public class Image {
@Element(name = "url")
private String mUrl;
public String getUrl() {
return mUrl;
}
@Element(name = "id")
private String mId;
public String getId() {
return mId;
}
@Element(name = "source_url")
public String mSourceUrl;
public String getSourceUrl() {
return mSourceUrl;
}
}
我应该如何完成这个?我认为添加带有条目 @ElementList(entry = "image")
的注释可以解决这个问题?
我的预感是正确的,我继续删除了 class Images.java
并在我的 Data.java
模型 class 中添加了以下注释:
@ElementList(name = "images", entry = "image")
private List<Image> mImages;
public List<Image> getImages() {
return mImages;
}
请注意,我将图像模型 @Root
更改为 @Element
class:
@Element(name = "image")
public class Image {
@Element(name = "url")
private String mUrl;
public String getUrl() {
return mUrl;
}
@Element(name = "id")
private String mId;
public String getId() {
return mId;
}
@Element(name = "source_url")
public String mSourceUrl;
public String getSourceUrl() {
return mSourceUrl;
}
}