java - 如何通过 simpleframework 解析带有没有父元素列表的 XML

标签 java xml simple-framework

<?xml version="1.0"?>
<list xmlns="http://namespace">
    <imageList>img1</imageList>
    <imageList>img2</imageList>
    <imageList>img3</imageList>
    <imageList>img4</imageList>
    <imageList>img5</imageList>
    <imageList>img6</imageList>
</list>

我有这样的内容的xml(不是真实的,只是为了描述我的问题)

想要通过 simpleframevork 将其解析为类似 List<String> 的内容

但是我之前尝试过的所有内容都会引发异常

我的解析类

@Root(strict = false)
public class List {

    @ElementList(entry = "imageList", type = String.class, empty = false)
    public ArrayList<String> images;

    public ArrayList<String> getImages() {
        return images;
    }
}

我收到异常

Caused by: org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(entry=imageList, data=false, inline=false, name=, type=class java.lang.String, required=true, empty=false) on field 'images' public java.util.ArrayList ru.cian.qa.pojo.List.images for class ru.cian.qa.pojo.List at line 2

最佳答案

parametr inline = true 解决了我的问题

这里是工作代码

@Root(strict = false)
public class ImagesList {

    @ElementList(entry = "imageList", inline = true)
    private List<String> images;

    public List<String> getImages() {
        return images;
    }
}

关于java - 如何通过 simpleframework 解析带有没有父元素列表的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36504872/

相关文章:

ruby-on-rails - 是否可以将 HTTParty 请求结果作为对象使用

php - 如何在 PHP 中从 .GPX 文件中读取值?

java - Joda-Time `DateTime` 对象到 `org.simpleframework.xml` ("Simple XML Serialization"库中字符串的转换器)

java - 如何解决 HTTP Status 500 - ContainerException java.lang.NoSuchMethodError 错误

java - hibernate 4 : Why i am getting the AnnotationException : Unknown mappedBy?

Android xml : stretch an element and wrap the other, 水平彼此相邻

java - 属性未在自定义 ArrayList 扩展类上序列化

android - Admob 与 Android 中的 simple-xml 集成?

java - Firebase Cloud Messaging 生成新的发件人 ID

java - Jackson Unmarshall 自定义对象而不是 LinkedHashMap