使用SimpleXML很容易解决问题。 我做错了什么?
我正在使用Java和SimpleXML
我需要用SimpleXML解析这个XML文件:
yutyutyu http://sofzh.miximages.com/java/idMagazine=1&resource=miniature 2 188 204 399 319 http://www.web.com 188 204 399 319 http://www.web.com
我得到了这个例外:
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)on field’area’public java.util.ArrayList com.Magazine.Page.areas for class com.Magazine.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 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 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; }
您必须在区域的ArrayList上放置required = false,XML的某些页面没有区域
@Root public class Page { @Attribute String src; @Attribute String id; @Attribute String thumbnail; @ElementList (required=false) public ArrayList areas; }
当我在XML文件中出现错误时(例如未标记的标记),会出现错误。 对于有同样问题并且来到这篇文章的人。
- 如何使用Jacksonvalidation重复的属性?
- Generics Hell:我可以使用generics构建TypeLiteral <Set >吗?
- Java错误 – net.ucanaccess.jdbc.UcanaccessSQLException:格式错误的字符串:’
- readobject方法抛出ClassNotFoundException
- 如何从派生类外部调用super方法(即:toString())
- ‘new’关键字在Java中实际上做了什么,我应该避免创建新对象吗?
- 用Java下载整个FTP目录(Apache Net Commons)
- 在Java中访问shell环境变量
- ImageIO读取的RGB值略微不同于其他方法