user2888993
user2888993

Reputation:

Value required exception while parsing data using simplexml on android

I am using simplexml parsing to fetch data from network. while parsing it show the below error.

error:

org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=false, name=, required=true, type=void) on field 'jobs' private java.util.List com.example.simpledataparsing.JobList.jobs for class com.example.simpledata.line2

xml file :

<?xml version="1.0" encoding="UTF-8" ?>
<joblist>
<job><id>75027</id><status>OPEN</status><customer>Manikandan</customer><address>asdf</address><city>salem</city><state>tn</state><zip>636005</zip><product>pipe</product><producturl></producturl><comments>asdf</comments></job>
</joblist>

pojo class: JobList.java

package com.example.simpledataparsing;

import java.util.List;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
@Element (name="joblist")
public class JobList {

    @ElementList
    private List<Job> jobs;

    public List<Job> getJobs() {
        return jobs;
    }

    public void setJobs(List<Job> jobs) {
        this.jobs = jobs;
    }


}

Job.java

package com.example.simpledataparsing;

import org.simpleframework.xml.Element;
@Element (name = "job")
public class Job {

    @Element
    private int id;

}

Upvotes: 2

Views: 5870

Answers (2)

Red M
Red M

Reputation: 2789

The solution suggested by @Sergii Zagriichuk worked fine for me. You need to specify Path for element.

Upvotes: 0

ollo
ollo

Reputation: 25350

You have to make two corrections:

Full implementation of Job class

The class is not fully implemented; you have more fields in your XML than in the actual class. This will fail the deserialization of the class.

Simply add all those fields to your class and set proper Annotations. Note, that producturl is marked with @Element(required = false) so there's no value required and it can be empty (as in the XML).

@Element(name = "job")
public class Job
{
    @Element
    private int id;
    @Element
    private String status;
    @Element
    private String customer;
    @Element
    private String address;
    @Element
    private String city;
    @Element
    private String state;
    @Element
    private String zip;
    @Element
    private String product;
    @Element(required = false)
    private String producturl;
    @Element
    private String comments;

    // ...
}

Correct the JobList annotation

The XML contains a inline liste, you have to set it inline in your class too.

@Element(name = "joblist")
public class JobList
{

    @ElementList(inline = true)
    private List<Job> jobs;

    // ...
}

Upvotes: 8

Related Questions