一尘不染

使用SimpleXML非常容易解决问题。我做错了什么?

java

我正在使用Java和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)在字段“
areas”上,第1行的类com.Magazine.Page的java.util.ArrayList com.Magazine.Page.areas

杂志有一系列页面,每个页面都有一个区域数组,每个区域都有一个动作类,其中包含更多内容。问题必须在Areas数组上,所以在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;
}

阅读 207

收藏
2020-12-03

共1个答案

一尘不染

您必须在区域的ArrayList上放置required = false,XML的某些页面没有区域

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