简体   繁体   English

SimpleXML Android错误ValueRequiredException

[英]SimpleXML Android Error ValueRequiredException

I am integrating SimpleXml deserialization but got exception every time. 我正在集成SimpleXml反序列化,但每次都遇到异常。 Don't know what is the exact reason. 不知道确切原因是什么。 Please check the details provided under this question. 请检查此问题下提供的详细信息。 Thanks in advance. 提前致谢。

Below are the model classes . 下面是模型类

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 Structure 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>

Log Cat Error 日志猫错误

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

Here are some things to do: 这里有一些事情要做:

Correct Annotation 正确的注释

Classes should use @Root not @Element . 类应该使用@Root而不是@Element

Use inline-lists 使用内联列表

This will add an extra image element for the list: 这将为列表添加一个额外的image元素:

@ElementList(name="image")  
private ArrayList<ImageClass> imgList = new ArrayList<ImageClass>();

like this: 像这样:

<images>
   <image>
      <image name="abc">
         <!-- ... -->
      </image>
   </image>
</images>

Therefore the attribute of image -element (the first is used!) is not available. 因此, image -element(使用第一个!)属性不可用。 Add inline = true to use inline-lists instead. 添加inline = true改为使用内联列表。

Element for version is not constructed 未构建版本元素

As in the xml above, there's no version -element. 与上面的xml中一样,没有version -element。

Duplicate Annotations 重复注释

The Annotations for x and y of class PixelDetail use same names: PixelDetail类的xy注释使用相同的名称:

    @Attribute(name = "x")
    private String x;

    @Attribute(name = "x") /* ! */
    private String y;

Copy & paste typo? 复制并粘贴错字? ;-) ;-)

Default constructor 默认构造函数

For (de)serialization, each class is required to have a default constructor. 对于(反)序列化,每个类都必须具有默认构造函数。 Visibility doesn't matter, even a private one is Ok. 可见性并不重要,即使是私人的也可以。


You can check the fixed code here: 您可以在此处检查固定代码:

Class ImageClass 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;
    }

}

Class ImagesModel 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;
    }

}

Class PixelDetail 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() { }

}

Class Version Version

@Root(name = "version")
public class Version
{
    @ElementList(name = "diff", inline = true)
    private ArrayList<PixelDetail> pixelsData = new ArrayList<PixelDetail>();

    public ArrayList<PixelDetail> getPixelsList()
    {
        return pixelsData;
    }

}

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

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