2012-03-22 33 views
7

我與Java和SimpleXML的SimpleXML很容易解決問題。我做錯了什麼?

工作,我需要解析用SimpleXML這個XML文件:

<magazine title="N˙mero 1" id="1"> 
    <description>yutyutyu</description> 
    <miniature>http://web.com/scripts/getImage.php?idMagazine=1&resource=miniature.jpg</miniature> 
    <summary>2</summary> 
    <pages> 
     <page src="http://web.com/scripts/getImage.php?idMagazine=1&resource=page_001.jpg" id="1" thumbnail="http://web.com/scripts/getImage.php?idMagazine=1&resource=thumbnail_001.jpg"> 
      <areas> 
       <area id="1"> 
        <top>188</top> 
        <left>204</left> 
        <width>399</width> 
        <height>319</height> 
        <action type="openBrowser">http://www.web.com</action> 
       </area> 
       <area id="2"> 
        <top>188</top> 
        <left>204</left> 
        <width>399</width> 
        <height>319</height> 
        <action type="openBrowser">http://www.web.com</action> 
       </area> 
      </areas> 
     </page> 
     <page src="http://web.com/scripts/getImage.php?idMagazine=1&resource=page_002.jpg" id="2" thumbnail="web.com/scripts/getImage.php?idMagazine=1&resource=thumbnail_002.jpg"/> 
     <page src="http://web.com/scripts/getImage.php?idMagazine=1&resource=page_003.jpg" id="3" thumbnail="web.com/scripts/getImage.php?idMagazine=1&resource=thumbnail_003.jpg"/> 
    </pages>  
</magazine> 

我得到這個異常:

03-22 16:02 :35.072:WARN/System.err(1931):org.simpleframework.xml.core.ValueRequiredException:無法滿足@ org.simpleframework.xml.ElementList(data = false,empty = true,entry =,inline = false,name =,required = true,type = void)public java.util.ArrayList com.Magazine.Page.areas for com.Magazi ne.Page at line 1

雜誌有一個頁面數組,每個頁面都有一個區域數組,每個區域都有一個動作類,它有更多的內容。問題必須在區域數組上,所以它在Page類中。

@Root (name="magazine") 
public class FullMagazine { 
    @Attribute 
    String title; 
    @Attribute 
    String id; 
    @Element 
    String description; 
    @Element 
    String miniature; 
    @Element 
    int summary; 
    @ElementList 
    public ArrayList<Page> pages; 

    public String getTitle() { 
     return title; 
    } 
    public String getId() { 
     return id; 
    } 
    public String getDescription() { 
     return description; 
    } 
    public Bitmap getMiniature() { 
     return Util.getRemoteBitmap(miniature); 
    } 

    public static FullMagazine Load(String xml){ 
     Serializer serializer = new Persister(); 
     try{ 
      return serializer.read(FullMagazine.class, xml); 
     }catch (Exception e) {e.printStackTrace();} 
     return null; //si llega aquÌ es que ha fallado. 
    } 
} 

@Root 
public class Page { 
    @Attribute 
    String src; 
    @Attribute 
    String id; 
    @Attribute 
    String thumbnail; 
    @ElementList 
    public ArrayList<Area> areas; 
} 

@Root 
public class Area { 
    @Attribute 
    String id; 
    @Element 
    int top; 
    @Element 
    int left; 
    @Element 
    int width; 
    @Element 
    int height; 
    @Element 
    Action action; 
} 

@Root 
public class Action { 
    @Attribute 
    String type;  

    String action; 
} 

回答

16

你必須把需要對區域的ArrayList =假,一些XML的頁面不具有區域

@Root 
public class Page { 
    @Attribute 
    String src; 
    @Attribute 
    String id; 
    @Attribute 
    String thumbnail; 
    @ElementList (required=false) 
    public ArrayList<Area> areas; 
} 
+0

就是這樣,顯然如果'required'屬性設置爲'true',那麼如果沒有找到給定的節點,則該庫會引發異常。謝謝! – 2012-03-22 15:58:00

+0

Congratz @NullPointerException! – 2017-05-23 16:49:42

0

當我在XML文件中發生錯誤時(例如未被標記的標記),就會顯示錯誤。 對於有同樣問題並且來到這篇文章的人。