2012-03-22 26 views
7

tôi đang làm việc với Java và SimpleXMLRất dễ dàng để giải quyết vấn đề với SimpleXML. Những gì tôi đang làm sai?

tôi cần phải phân tích tập tin XML này với SimpleXML:

<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> 

tôi nhận được ngoại lệ này:

03-22 16:02 : 35.072: WARN/System.err (1931): org.simpleframework.xml.core.ValueRequiredException: Không thể thỏa mãn @ org.simpleframework.xml.ElementList (dữ liệu = false, empty = true, entry =, inline = false, name =, required = true, type = void) trên trường 'public' public java.util.ArrayList com.Magazine.Page.areas cho lớp com.Magazi ne.Page tại dòng 1

Tạp chí có một loạt các trang và mỗi trang có một số lớp hành động, có một số nội dung hơn. Vấn đề phải nằm trên mảng vùng, vì vậy nó nằm trong lớp 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; 
} 

Trả lời

16

bạn phải đặt yêu cầu = false vào ArrayList của khu vực, một số các trang của XML không có khu vực

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

Đó là nó, rõ ràng nếu thuộc tính 'required' được đặt thành' true', thư viện đã tăng ngoại lệ nếu một nút đã cho không được tìm thấy. Cảm ơn! –

+0

Congratz @NullPointerException! –

0

Lỗi đó xuất hiện khi tôi có lỗi trong tệp XML (như thẻ không được gắn). Đối với người có cùng vấn đề và đến bài đăng này.

Các vấn đề liên quan