【问题标题】:Value required exception while parsing data using simplexml on android在android上使用simplexml解析数据时需要值异常
【发布时间】:2013-10-17 04:58:06
【问题描述】:

我正在使用 simplexml 解析从网络中获取数据。在解析它时显示以下错误。

错误:

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 文件:

<?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 类: 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;

}

【问题讨论】:

标签: android simple-framework


【解决方案1】:

你必须做两个更正:

Job 类的完整实现

类没有完全实现;您的 XML 中的字段比实际类中的字段多。这将使类的反序列化失败。

只需将所有这些字段添加到您的类并设置适当的注释即可。请注意,producturl 标有 @Element(required = false),因此不需要任何值,它可以为空(如 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;

    // ...
}

更正 JobList 注释

XML 包含一个内联列表,你也必须在你的类中设置它inline

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

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

    // ...
}

【讨论】:

    【解决方案2】:

    @Sergii Zagriichuk 建议的解决方案对我来说效果很好。 您需要为元素指定路径。

    【讨论】:

      猜你喜欢
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-09
      • 1970-01-01
      相关资源
      最近更新 更多