XML Deserialize Java

99 views Asked by At

I have this XML format:

<NewDataSet>
  <Users>
    <mGUID>FA705D4B-9CC9-8548-0050560039A2</mGUID>
    <mName>ADRIAN</mName>
    <mIDCard>A93</mIDCard>
    <mIsWarehouseKeeper>false</mIsWarehouseKeeper>
    <mWarehouseGUID>3C58C2E9-9B88-B17D-002127DE1AED</mWarehouseGUID>
  </Users>
  <Users>
    <mGUID>4936C006-8E4D-0050560039A2</mGUID>
    <mName>__ANDREI</mName>
    <mIDCard>A198</mIDCard>
    <mIsWarehouseKeeper>false</mIsWarehouseKeeper>
    <mWarehouseGUID>3C58C2E9-B17D-002127DE1AED</mWarehouseGUID>
  </Users>
  <Users>
    <mGUID>C2D82C1D-8E8D-0050560039A2</mGUID>
    <mName>PETRU</mName>
    <mIDCard>A471</mIDCard>
    <mIsWarehouseKeeper>false</mIsWarehouseKeeper>
    <mWarehouseGUID>3C58C2E9-B17D-002127DE1AED</mWarehouseGUID>
  </Users>
</NewDataSet>

I try to deserialize with Simple XML:

@Root(strict=false)
public class NewDataSet
{
    @ElementList(entry="Users")
    private ArrayList<Users> Users;
}

And Users:

@Root
public class Users
{
    @Element
    private String mWarehouseGUID;
    @Element
    private String mIsWarehouseKeeper;
    @Element
    private String mGUID;
    @Element
    private String mName;
    @Element
    private String mIDCard;

    public String getMWarehouseGUID ()
    {
        return mWarehouseGUID;
    }

    public void setMWarehouseGUID (String mWarehouseGUID)
    {
        this.mWarehouseGUID = mWarehouseGUID;
    }

    public String getMIsWarehouseKeeper ()
    {
        return mIsWarehouseKeeper;
    }

    public void setMIsWarehouseKeeper (String mIsWarehouseKeeper)
    {
        this.mIsWarehouseKeeper = mIsWarehouseKeeper;
    }

    public String getMGUID ()
    {
        return mGUID;
    }

    public void setMGUID (String mGUID)
    {
        this.mGUID = mGUID;
    }

    public String getMName ()
    {
        return mName;
    }

    public void setMName (String mName)
    {
        this.mName = mName;
    }

    public String getMIDCard ()
    {
        return mIDCard;
    }

    public void setMIDCard (String mIDCard)
    {
        this.mIDCard = mIDCard;
    }

    @Override
    public String toString()
    {
        return "ClassPojo [mWarehouseGUID = "+mWarehouseGUID+", mIsWarehouseKeeper = "+mIsWarehouseKeeper+", mGUID = "+mGUID+", mName = "+mName+", mIDCard = "+mIDCard+"]";
    }
}

I get this exception:

org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Element(data=false, name=, required=true, type=void) on field 'mGUID' private java.lang.String net.daniftodi.memoexpress.model.Users.mGUID for class net.daniftodi.memoexpress.model.Users at line 153

I think the problem is in annotations, please help me to deserialize NewDataSet -> Users to java Object with array of Users.

Thank you.

1

There are 1 answers

0
manish On

@ElementList(entry="Users", inline=true) will get rid of the error since each Users tag in the XML represents a user, there can be more than one of them and they are not wrapped in another container element.