元素'结果'也是类中的路径名

时间:2015-04-08 00:15:53

标签: java android simple-framework

我正在xml文档下面集成SimpleXml反序列化,但每次都有异常。不知道究竟是什么原因。

但我的代码显示“ElementException”。

如果我在List上面注释了@Path(“results”),那么控制台会显示valueRequiredException。

我该怎么办?

<xten>
    <status>
         <return_code>200</return_code>
    </status>
    <results>
        <total_count>40</total_count>
        <npp>5</npp>
        <result type="PRRT" no="1" docid="64458" count="37">
             <field name="DISPTEL">11111111111</field>
             <field name="UP_CD">65789</field>
        </result>
        <result type="PRRT" no="1" docid="64458" count="37">
             <field name="DISPTEL">11111111111</field>
             <field name="UP_CD">65789</field>
        </result>
        <result type="PRRT" no="1" docid="64458" count="37">
             <field name="DISPTEL">11111111111</field>
             <field name="UP_CD">65789</field>
        </result>
    </results>
</xten>


@Root(strict=false)
public class SearchInfosXten {

    @Path("results")
    @Element(name = "total_count")
    private int totalCount;
    @Path("results")
    @Element(name = "npp")
    private int npp;

    @ElementList(name = "results", entry = "result")
    private List<SearchResult> result;

    public int getTotalCount() {
        return totalCount;
    }

    public int getNpp() {
        return npp;
    }

    public List<SearchResult> getResults() {
        return result;
    }
}

public class SearchResult {

    @Attribute(name = "type")
    private String type;

    @Attribute(name = "no")
    private String no;

    @Attribute(name = "docid")
    private String docId;

    @Attribute(name = "count")
    private String count;

    @ElementList
    private List<Item> items;

    public String getType() {
        return type;
    }

    public String getNo() {
        return no;
    }

    public String getDocId() {
        return docId;
    }

    public String getCount() {
        return count;
    }

    public List<Item> getItems() {
        return items;
    }

    public class Item {
        @Attribute(name = "name")
        public String name;

        @Text(data=true)
        public String text;

    }

}

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。尝试使用@ElementList(entry =&#34; result&#34;,inline = true)

请查看以下代码。

@Root(strict=false)
public class SearchInfosXten {

    @Element(name = "results")
    private Results results;

    public Results getResults() {
        return results;
    }
}

@Root(strict = false)
public class Results {

    @Element(name = "total_count")
    private int totalCount;

    @ElementList(entry = "result", inline = true)
    private List<Result> result;

    public int getTotalCount() {
        return totalCount;
    }

    public List<Result> getResult() {
        return result;
    }

}

@Root(strict = false)
public class Result {

    @Attribute(name = "type")
    private String type;

    @Attribute(name = "no")
    private int no;

    @Attribute(name = "docid")
    private int docId;

    @Attribute(name = "count")
    private int count;

    @ElementMap(entry = "field", key = "name", attribute = true, inline = true)
    private Map<String, String> fields;

    public String getType() {
        return type;
    }

    public int getNo() {
        return no;
    }

    public int getDocId() {
        return docId;
    }

    public int getCount() {
        return count;
    }

    public List<Item> getItems() {
        return items;
    }

    public Map<String, String> getFields() {
    return fields;
    }

}