Very easy to solve issue with SimpleXML. What i#39;m doing wrong?(SimpleXML 很容易解决问题.我做错了什么?)
问题描述
我正在使用 Java 和 SimpleXML
I'm working with Java and SimpleXML
我需要用 SimpleXML 解析这个 XML 文件:
I need to parse this XML file with 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>
我遇到了这个异常:
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 'areas' public java.util.ArrayList com.Magazine.Page.areas for class com.Magazine.Page 在第 1 行
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
杂志有一个页面数组,每个页面有一个区域数组,每个区域都有一个动作类,里面有更多的内容.问题肯定出在area数组上,所以在Page类中.
Magazine has an array of pages, and each page has an array of areas, and each area has a action class, which has some more content. The problem must be on areas array, so it is in Page class.
@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;
}
推荐答案
必须在area的ArrayList上加上required=false,XML的部分Pages没有Areas
You must put required=false on the ArrayList of areas, some of the Pages of the XML doesn't have Areas
@Root
public class Page {
@Attribute
String src;
@Attribute
String id;
@Attribute
String thumbnail;
@ElementList (required=false)
public ArrayList<Area> areas;
}
这篇关于SimpleXML 很容易解决问题.我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SimpleXML 很容易解决问题.我做错了什么?
基础教程推荐
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01