Java Simple-XML反序列化

时间:2012-03-30 00:47:38

标签: java android xml xml-deserialization

我正在尝试反序列化以下XML:

<jdownloader>
    <package
        package_eta="~"
        package_linksinprogress="0"
        package_linkstotal="5"
        package_loaded="0 B"
        package_name="Unchecked"
        package_percent="0.00"
        package_size="0 B"
        package_speed="0 B"
        package_todo="0 B" >

        <file
            file_hoster="hoster_name"
            file_name="name"
            file_package="Unchecked"
            file_percent="0.09"
            file_speed="0"
            file_status="[Aborted] " >

            <file
                file_hoster="hoster_name"
                file_name="name"
                file_package="Unchecked"
                file_percent="0.03"
                file_speed="0"
                file_status="[Aborted] " >

                <file
                    file_hoster="hoster_name"
                    file_name="name"
                    file_package="Unchecked"
                    file_percent="0.05"
                    file_speed="0"
                    file_status="[Aborted] " >
                </file>
            </file>
        </file>
    </package>
</jdownloader>

我似乎无法使用simple-xml以我想要的方式反序列化。

我需要获取包的对象(每个xml中将包含多个包),以及作为包对象的子包的文件对象列表。 XML无法更改,似乎每个文件元素都嵌套在前一个文件元素下,而不是在父包下面。

到目前为止我的代码是:

@Root(name = "jdownloader")
public class DownloadsModel {

    @ElementList(name="package")
    public List<PackageModel> Package;

}


@Root(name = "package")
public class PackageModel {

    @ElementList(name="file")
    public List<FileModel> file;

    @Attribute
    public String package_eta;

    @Attribute
    public String package_linksinprogress;

    @Attribute
    public String package_linkstotal;

    @Attribute
    public String package_loaded;

    @Attribute
    public String package_name;

    @Attribute
    public String package_percent;

    @Attribute
    public String package_size;

    @Attribute
    public String package_speed;

    @Attribute
    public String package_todo;

}

@Root(name = "file")
public class FileModel {

    @Attribute
    public String file_hoster;

    @Attribute
    public String file_name;

    @Attribute
    public String file_package;

    @Attribute
    public String file_percent;

    @Attribute
    public String file_speed;

    @Attribute
    public String file_status;

}

当Simple-XML尝试反序列化xml时,它会在包类中查找文件元素的属性。

有人能帮助我吗?

2 个答案:

答案 0 :(得分:0)

首先,尝试更换:

public class PackageModel {

         @ElementList(name="file")
         public List<FileModel> file;

用这个

public class PackageModel {

         @ElementList(name="file", inline=true)
         public List<FileModel> file;

然后,您可以取消多个FileModel定义。

更多详情here

答案 1 :(得分:0)

您的模型就像

@Root(name = "package")
public class DownloadsModel {

    @ElementList(name = "package", inline = true)
    public List<PackageModel> Package;
}

包装:

@Root(name = "package")
public class PackageModel {

    @ElementList(name = "file", inline = true)
    public List<FileModel> file;
    ....
}

和for file(XML无法更改,似乎每个文件元素都嵌套在前一个文件元素下,而不是在父包下面的所有文件元素)

@Root(name = "file")
public class FileModel {

@ElementList(name = "file", inline = true, required = false)
public List<FileModel> file;

@Attribute
public String file_hoster;

....
}