我正在整合SimpleXml反序列化,但每次都有异常。不知道究竟是什么原因。请查看此问题下提供的详细信息。提前谢谢。
以下是模型类 。
import java.util.ArrayList;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
@Root(name = "images")
public class ImagesModel {
@ElementList(name="image")
private ArrayList<ImageClass> imgList = new ArrayList<ImageClass>();
public ArrayList<ImageClass> getImageList(){
return imgList;
}
}
import java.util.ArrayList;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
@Element(name = "image")
public class ImageClass {
@Attribute(name = "name")
private String imageName;
public String getImageName(){
return imageName;
}
public ImageClass(String imageName) {
this.imageName = imageName;
}
@ElementList(name="ver")
private ArrayList<Version> versions = new ArrayList<Version>();
public ArrayList<Version> getVersionsList(){
return versions;
}
}
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
@Element(name = "diff")
public class PixelDetail{
@Attribute(name = "x")
private String x;
public String getX(){
return x;
}
@Attribute(name = "x")
private String y;
public String getY(){
return y;
}
public PixelDetail(String x, String y) {
this.x = x;
this.y = y;
}
}
import java.util.ArrayList;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
@Element(name = "version")
public class Version{
@ElementList(name="diff")
private ArrayList<PixelDetail> pixelsData = new ArrayList<PixelDetail>();
public ArrayList<PixelDetail> getPixelsList(){
return pixelsData;
}
}
XML结构
<images>
<image name="Org1" >
<version>
<ver>
<diff
x="79"
y="19" />
<diff
x="215"
y="73" />
<diff
x="68"
y="202" />
<diff
x="96"
y="289" />
<diff
x="173"
y="164" />
</ver>
<ver>
<diff
x="14 "
y="93 " />
<diff
x="57 "
y="46 " />
<diff
x=" 192"
y="17" />
<diff
x="180 "
y="139" />
<diff
x="135 "
y="242 " />
</ver>
<ver>
<diff
x="286 "
y="123" />
<diff
x="31"
y="292" />
<diff
x="170"
y="216" />
<diff
x="189"
y="234" />
<diff
x="131 "
y="286" />
</ver>
</version>
</image>
<image name="Org2" >
<version>
<ver>
<diff
x="197"
y="41" />
<diff
x="269"
y="162" />
<diff
x="297"
y="19" />
<diff
x="4"
y="52" />
<diff
x="178"
y="284" />
</ver>
<ver>
<diff
x="213"
y="59" />
<diff
x="208"
y="97" />
<diff
x="284"
y="162" />
<diff
x="193"
y="138" />
<diff
x="5"
y="221" />
</ver>
<ver>
<diff
x="219"
y="97" />
<diff
x="40"
y="44" />
<diff
x="67"
y="21" />
<diff
x="181"
y="37" />
<diff
x="208"
y="172" />
</ver>
</version>
</image>
</images>
记录Cat错误
09-11 14:52:55.926: W/System.err(5525): org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Attribute(empty=, name=name, required=true) on field 'imageName' private java.lang.String com.wavinno.iotasol.spotdifference.models.ImageClass.imageName for class com.wavinno.iotasol.spotdifference.models.ImageClass at line 5
答案 0 :(得分:0)
以下是一些可以做的事情:
课程应使用@Root
而不是@Element
。
这将为列表添加额外的image
元素:
@ElementList(name="image")
private ArrayList<ImageClass> imgList = new ArrayList<ImageClass>();
像这样:
<images>
<image>
<image name="abc">
<!-- ... -->
</image>
</image>
</images>
因此image
- 元素的属性(第一个被使用!)不可用。添加inline = true
代替使用内联列表。
与上面的xml一样,没有version
- 元素。
类x
的{{1}}和y
的注释使用相同的名称:
PixelDetail
复制&amp;粘贴错字? ; - )
对于(反)序列化,每个类都需要具有默认构造函数。可见性并不重要,即使是私人也是如此。
您可以在此处查看固定代码:
@Attribute(name = "x")
private String x;
@Attribute(name = "x") /* ! */
private String y;
ImageClass
@Root(name = "image")
public class ImageClass
{
@Attribute(name = "name")
private String imageName;
public String getImageName()
{
return imageName;
}
public ImageClass(String imageName)
{
this.imageName = imageName;
}
ImageClass() { }
@ElementList(name = "version")
private ArrayList<Version> versions = new ArrayList<Version>();
public ArrayList<Version> getVersionsList()
{
return versions;
}
}
ImagesModel
@Root(name = "images")
public class ImagesModel
{
@ElementList(name = "image", inline = true)
private ArrayList<ImageClass> imgList = new ArrayList<ImageClass>();
public ArrayList<ImageClass> getImageList()
{
return imgList;
}
}
PixelDetail
@Root(name = "diff")
public class PixelDetail
{
@Attribute(name = "x")
private String x;
public String getX()
{
return x;
}
@Attribute(name = "y")
private String y;
public String getY()
{
return y;
}
public PixelDetail(String x, String y)
{
this.x = x;
this.y = y;
}
PixelDetail() { }
}
Version