元素 'results' 也是 class 中的路径名

Element 'results' is also a path name in class

我在 xml 文档下方集成了 SimpleXml 反序列化,但每次都出现异常。不知道具体原因是什么。

但我的代码显示 "ElementException"。

如果我在列表上方注释了@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;

    }

}

我找到了解决方案。尝试使用 @ElementList(entry = "result", 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;
    }

}