如何通过 simpleframework 解析 XML 没有父级的元素列表
how to parse XML with list of elements without parent via simpleframework
<?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>
但我之前尝试的所有操作都抛出异常
我的class 用于解析
@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
参数inline = true
解决了我的问题
工作代码在这里
@Root(strict = false)
public class ImagesList {
@ElementList(entry = "imageList", inline = true)
private List<String> images;
public List<String> getImages() {
return images;
}
}
<?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>
但我之前尝试的所有操作都抛出异常
我的class 用于解析
@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
参数inline = true
解决了我的问题
工作代码在这里
@Root(strict = false)
public class ImagesList {
@ElementList(entry = "imageList", inline = true)
private List<String> images;
public List<String> getImages() {
return images;
}
}