简体   繁体   English

使用SimpleFramework反序列化此XML数组

[英]Deserializing this XML Array with SimpleFramework

I am trying to deserialize this XML Array with SimpleFramework , and I keep running into this issue: 我试图使用SimpleFramework反序列化此XML数组, SimpleFramework我一直遇到这个问题:

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 07-04 16:14:59.840 1681-1681 / com.sampleapp D / SampleApp:org.simpleframework.xml.core.ElementException:元素'image'在com.todocodepathapp.api.models.Images类中不匹配5号线

This is the xml array being returned from an API: 这是从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>

And these are the model classes I am using (I have a hunch that I shouldn't be using Images.java , and there should only be three classes): 这些是我正在使用的模型类(我有一种直觉,我不应该使用Images.java ,并且应该只有三个类):

Response.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 Images.java

@Root(name = "images")
public class Images {

    @ElementList(entry = "image")
    private List<Image> mImageList;
    public List<Image> getImageList() {
        return mImageList;
    }

}

Image.java 图像.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;
    }

}

How should I be accomplishing this? 我应该如何做到这一点? I thought that adding the annotation with the entry @ElementList(entry = "image") would solve this? 我认为添加带有@ElementList(entry = "image")的注释将解决此问题?

My hunch was correct, and I went ahead and deleted the class Images.java and added the following annotations to my Data.java model class: 我的预感是正确的,因此我继续删除了Images.java类,并在Data.java模型类中添加了以下注释:

@ElementList(name = "images", entry = "image")
private List<Image> mImages;
public List<Image> getImages() {
    return mImages;
}

One note is that I changed @Root to @Element for the Image model class: 请注意,我将Image模型类的@Root更改为@Element

@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;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM