简体   繁体   中英

SimpleXML Android Error ValueRequiredException

I am integrating SimpleXml deserialization but got exception every time. 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

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

Use inline-lists

This will add an extra image element for the list:

@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. Add inline = true to use inline-lists instead.

Element for version is not constructed

As in the xml above, there's no version -element.

Duplicate Annotations

The Annotations for x and y of class PixelDetail use same names:

    @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

@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

@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

@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

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

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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