Java 使用 SimpleXML 很容易解决问题。我在做什么错?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9825380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Very easy to solve issue with SimpleXML. What i'm doing wrong?
提问by Alex Salom
i'm working with Java and SimpleXML
我正在使用 Java 和 SimpleXML
I need to parse this XML file with 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>
I'm getting this exception:
我收到此异常:
03-22 16:02:35.072: WARN/System.err(1931): org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=false, name=, required=true, type=void) on field 'areas' public java.util.ArrayList com.Magazine.Page.areas for class com.Magazine.Page at line 1
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' public java.util.ArrayList com.Magazine.Page.areas for class com.Magazine.Page at line 1
Magazine has an array of pages, and each page has an array of areas, and each area has a action class, wich has some more content. The problem must be on areas array, so it is in Page class.
杂志有一个页面数组,每个页面有一个区域数组,每个区域都有一个动作类,其中有更多的内容。问题一定出在 area 数组上,所以它在 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;
}
采纳答案by NullPointerException
You must put required=false on the ArrayList of areas, some of the Pages of the XML doesn't have Areas
你必须把required=false放在区域的ArrayList上,XML的一些页面没有区域
@Root
public class Page {
@Attribute
String src;
@Attribute
String id;
@Attribute
String thumbnail;
@ElementList (required=false)
public ArrayList<Area> areas;
}
回答by raiym
That errors appear when I had errors in XML file (like unended tag). For someone who have the same problem and came to this post.
当我在 XML 文件中出现错误(如未结束的标签)时,就会出现该错误。对于有同样问题并来到这篇文章的人。