SimpleXml ValueRequiredException

时间:2015-04-27 20:59:56

标签: java android simple-framework

我正在尝试使用SimpleXml 2.7.1进行复杂的XML解析。

我得到的错误是

org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Element(name=, type=void, data=false, required=true) on field 'RWRD_NMB' private java.lang.String com.staples.mobile.enterprise.rewards.model.core.simple.Table1.RWRD_NMB for class com.staples.mobile.enterprise.rewards.model.core.simple.Table1 at line 1

在以下测试中:

@Test
public void table1ShouldGetParsed() {
    String rawXmlResponse = "<diffgr:diffgram xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\">\n" +
            "                  <NewDataSet xmlns=\"\">\n" +
            "                     <Table1 diffgr:id=\"Table11\" msdata:rowOrder=\"0\">\n" +
            "                        <RWRD_NMB>5094934675</RWRD_NMB>\n" +
            "                        <TRAN_ID>304011</TRAN_ID>\n" +
            "                        <TRAN_DT>2015-02-23T00:00:00-05:00</TRAN_DT>\n" +
            "                        <STR_NMB>1715</STR_NMB>\n" +
            "                        <SKU_NMB>648370</SKU_NMB>\n" +
            "                        <ITEM_DESCRIPTION>648370 - EPSON T200 COLOR INK 3PK</ITEM_DESCRIPTION>\n" +
            "                        <DISC_PROG_ID>1</DISC_PROG_ID>\n" +
            "                        <CATEGORY_ID>1</CATEGORY_ID>\n" +
            "                        <CPN_AMT>0.00</CPN_AMT>\n" +
            "                        <TOTAL_QTY>1</TOTAL_QTY>\n" +
            "                        <TOT_PRICE_AMT>22.49</TOT_PRICE_AMT>\n" +
            "                        <NET_SPND_AMT>22.49</NET_SPND_AMT>\n" +
            "                        <TOTAL_REWARDS>0.00</TOTAL_REWARDS>\n" +
            "                        <DSC_PGM_DSCR>Rewards core program</DSC_PGM_DSCR>\n" +
            "                     </Table1>\n" +
            "                  </NewDataSet>\n" +
            "               </diffgr:diffgram>";
    Serializer serializer = new Persister();
    try {
        Table1 table1 = serializer
                .read(Table1.class, rawXmlResponse, false);
        Assert.assertNotNull(table1);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}

Table1.java:

@Root(name = "Table1")
@NamespaceList({
        @Namespace(reference = "urn:schemas-microsoft-com:xml-msdata"),
        @Namespace(reference = "urn:schemas-microsoft-com:xml-diffgram-v1")
})

public class Table1 {

@Attribute(required = false)
private String id;
@Attribute(required = false)
private String rowOrder;

@Element
private String RWRD_NMB;
@Element
private String TRAN_ID;
@Element
private String TRAN_DT;
@Element
private String STR_NMB;
@Element
private String SKU_NMB;
@Element
private String ITEM_DESCRIPTION;
@Element
private String DISC_PROG_ID;
@Element
private String CATEGORY_ID;
@Element
private String CPN_AMT;
@Element
private int TOTAL_QTY;
@Element
private float TOT_PRICE_AMT;
@Element
private float NET_SPEND_AMT;
@Element
private float TOTAL_REWARDS;
@Element
private String DSC_PGM_DSCR;

public String getRewardsNumber() {
    return RWRD_NMB;
}
//more getters
}

NewDataSet.java:

@Root(name = "NewDataSet", strict = false)
@Namespace
public class NewDataSet {
    @ElementListUnion({
            @ElementList(entry = "Table", type = Table.class, inline = true),
            @ElementList(entry = "Table1", type = Table1.class, inline = true)
    })
    private List<Object> list;

    public NewDataSet(){
        list = new ArrayList<>();
    }

    public List<Object> getList() {
        return list;
    }

    public void setList(List<Object> list) {
        this.list = list;
    }
}

Diffgram.java:

@Root(name = "diffgram")
@NamespaceList({
        @Namespace(prefix = "msdata", reference = "urn:schemas-microsoft-com:xml-msdata"),
        @Namespace(prefix = "diffgr", reference = "urn:schemas-microsoft-com:xml-diffgram-v1")
})

public class Diffgram {
@Element(name = "NewDataSet")
private NewDataSet newDataSet;

public NewDataSet getNewDataSet() {
    return newDataSet;
}
}

我已经为Table(一个非常类似于Table1的类,只是一些不同的成员),NewDataSet和Diffgram编写了类似的测试,所有这些测试都给了我ValueRequiredExceptions。我觉得我已经盯着这个太久了,我在这里缺少一些基本的东西!有一种强烈的感觉,如果我把其中一个弄清楚,剩下的就会落到实处。

0 个答案:

没有答案