3

我尝试使用SimpleXML库解析 xml,但出现以下异常;当我将水域定义为列表时,无法弄清楚为什么它会抱怨。可能我忽略了一些非常基本的东西,但我找不到它。我几乎遵循http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#list上给出的示例模式

顺便说一句:StackOverflow 上有一个类似的问题,但情况不同,实际上并没有处理 ElementLists。就我而言,我肯定想要一个 ElementList,因此会假设我的元素的多重存在实际上应该没问题。

错误日志:

Error parsing xml.
        org.simpleframework.xml.core.PersistenceException: Element 'gewaessereintrag' declared twice at line 9
        at org.simpleframework.xml.core.Variable$Adapter.read(Variable.java:456)
....

XML:

<?xml version="1.0" ?>
<gewaesser>
    <returncode>0</returncode>
    <gewaessereintrag>
        <id>1</id>
        <name><![CDATA[Entry1]]></name>
        <info><![CDATA[Info1.]]></info>
    </gewaessereintrag>
    <gewaessereintrag>
        <id>2</id>
        <name><![CDATA[Entry2]]></name>
        <info><![CDATA[Info2.]]></info>
    </gewaessereintrag>
</gewaesser>

WaterList(处理<gewaesser>):

@Root(name = "gewaesser")
public class WaterList {

    @ElementList(type = Water.class, name = "gewaessereintrag")
    private List<Water> waters;

    @Element(name = "returncode")
    private String returncode;

    public List<Water> getWaters() {
        return waters;
    }
}

水(处理<gewaessereintrag>):

@Root(name = "gewaessereintrag")
public class Water {

    @Element(required = false, name = "name")
    private String name;

    @Element(required = false, name = "info")
    private String info;

    @Element(required = false, name = "id", type = Long.class)
    private Long id;

}
4

1 回答 1

4

我自己找到了答案,但也许这对其他人有帮助:

我忘了声明“ inline = true

@ElementList(type = Water.class, name = "gewaessereintrag", inline = true)
于 2012-05-16T10:52:29.447 回答