我有这个代码:
@ElementList(name="package", required=false, inline=true)
private Hashtable<String, PackageListItem> packages;
这给了我这个错误:
D/debug(22150): java.util.Hashtable cannot be cast to java.util.Collection
我尝试使用 getter 来解决这个问题:
@ElementList(name="package", required=false, inline=true)
private Collection<PackageListItem> getPackageXML() {
return packages.values();
}
但这给了我一个我不太明白的错误:
D/debug(22307): Default constructor can not accept read only @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=true, name=package, required=false, type=void) on method 'packageXML' in class com.example.MyClass
我知道我可以使用 ElementMap
,但这在我的 XML 中提供了太多信息。
如何在 Simple 2.7.1 中序列化 Hashtable
的值?
最佳答案
或者,您可以使用 HashMap 代替 Hashtable。它们几乎相同,因此很有可能适合您。以下是差异: Differences between HashMap and Hashtable?
下面是使用 simpleXML 映射的示例: http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#map
更新:
嗯,Hashtable 实现了 Map 接口(interface),因此它应该与 @ElementMap 注解一起使用。 (您现在使用@ElementList。)
关于java - 使用 org.simpleframework.xml 序列化哈希表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23992186/