2012-05-16 60 views
3

我嘗試解析xml與SimpleXML庫,我得到以下異常;當我把我的水域定義爲一個列表時,無法弄清楚它爲什麼會抱怨。 我可能忽略了一些非常基本的東西,但我無法找到它。我幾乎下面就http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#listSimpleXML:聲明兩次的元素

BTW給出的例子的模式:有一個similar question這裏在計算器上,但它是一個不同的情況下,沒有實際處理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

我只是發現自己的答案,但也許這是對別人有幫助的:

我忘了申報「inline = true

@ElementList(type = Water.class, name = "gewaessereintrag", inline = true)