2014-02-05 39 views
3

我的問題很簡單,但我無法找到它。元素列表中的空條目SimpleXML

我有一個列表類和XML序列化的入門級:

@Root(name = "entries") 
public class List { 

    @ElementList(required = false, entry = "entry", inline = true, empty = true) 
    private List<Entry> entries; 
} 

@Root 
public class Entry { 

    @Element(name = "entry_id", required = true) 
    private long id; 

    @Element(name = "text", required = true) 
    private String Text; 
} 

我試圖解析這個XML,它沒有在列表中的條目:

<entries> 
    <entry /> 
<entries> 

返回以下錯誤:

W/System.err(3335): org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.Element(data=false, name=entry_id, required=true, type=void) on field 'id' private long com.android.apps.model.Entry.id for class com.android.apps.model.Entry at line 2 

我在做什麼錯? ElementList被設置爲空= true並且必需= false。 有人可以幫忙嗎?

+0

它確實有一個列表,它雖然空單,因此未通過檢查有一個id屬性。所以你真正的問題是爲什麼輸出中的空標籤? –

+0

我明白了。 XML由另一個系統生成。在這種情況下,我想知道如果簡單的框架有一些方法來識別列表中的空項目並忽略它。有沒有辦法? –

+0

不是我所知道的,也不能想到有它的理由。唯一我能想到的。是1)報告這是一個錯誤2)修復Xml反序列化之前。 –

回答

0

您可以檢查手動空元素:

@Root(name = "entries") 
@Convert(List.ListConverter.class) // Set the converter 
public class List 
{ 
    @ElementList(required = false, entry = "entry", inline = true, empty = true) 
    private java.util.List<Entry> entries; 


    public void add(Entry e) 
    { 
     // Just for testing 
     entries.add(e); 
    } 



    static class ListConverter implements Converter<List> 
    { 
     @Override 
     public List read(InputNode node) throws Exception 
     { 
      List l = new List(); 
      InputNode child = node.getNext("entry"); 

      while(child != null) 
      { 
       if(child.isEmpty() == true) // child is an empty tag 
       { 
        // Do something if entry is empty 
       } 
       else // child is not empty 
       { 
        Entry e = new Persister().read(Entry.class, child); // Let the Serializer read the Object 
        l.add(e); 
       } 

       child = node.getNext("entry"); 
      } 

      return l; 
     } 


     @Override 
     public void write(OutputNode node, List value) throws Exception 
     { 
      // Not required for reading ... 
      throw new UnsupportedOperationException("Not supported yet."); 
     } 
    } 
} 

如何使用:

Serializer ser = new Persister(new AnnotationStrategy()); // Set AnnotationStrategy here! 
List l = ser.read(List.class, yourSourceHere); 

文檔:

+0

如果列表中完全填充空標籤,則此解決方案不起作用。你能改善你的答案嗎? – GreenThor

0

爲了避免解析錯誤不應該把註釋標籤@setË@get

@Root(name = "entries", strict = false) 
public class List { 

@set:ElementList(required = false, entry = "entry", inline = true, empty = true) 
@get:ElementList(required = false, entry = "entry", inline = true, empty = true) 
    private List<Entry> entries; 

} 

@Root 
public class Entry { 

    @set:Element(name = "entry_id", required = true) 
    @get:Element(name = "entry_id", required = true) 
    private long id; 

    @set:Element(name = "text", required = true) 
    @get:Element(name = "text", required = true) 
    private String Text; 
} 
+0

你應該用英文在你的答案上寫上你的答案 – Heyji