我正在使用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类:
JavaList.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;
    }


}

JavaJAVA
package com.example.simpledataparsing;

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

    @Element
    private int id;

}

最佳答案

你必须做两个修正:
作业类的全面实施
类没有完全实现;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;

    // ...
}

更正作业列表批注
XML包含内联列表,您还必须在类中设置它inline
@Element(name = "joblist")
public class JobList
{

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

    // ...
}

关于android - 在Android上使用simplexml解析数据时,值必填异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19418612/

10-09 03:51