我正在尝试使用 simplexml 来反序列化一个对象,而使用 java 的代码在 Android 上不起作用。
简单类:
@Root
public class GenericContainer
{
@Element
public MainEntity el;
}
即声明:
GenericContainer genContainer = new GenericContainer();
genContainer.el = new SubClassBEntity(); //SubClassBEntity is a Subclass of MainEntity
被序列化并生成一个xml:
<?xml version='1.0' encoding='utf-8'?>
<genericContainer>
<el class="test.entities.SubClassBEntity>
<x>1</x>
</el>
</genericContainer>
如果我尝试使用 java 反序列化 xml,一切正常,但如果我使用 android 执行相同操作,我会发生崩溃,异常消息为 test.entities.SubClassBEntity
。
有解决这个问题的办法吗? 谢谢
最佳答案
我找到的唯一解决方案是使用 @ElementUnion
将所有可能性放入其中。这是一个肮脏的解决方案,因为你可以有一个包含 50 个子类的基类,你将不得不把所有的。自动重构不会更改注释,这将是一段容易出错的代码。
关于java - android继承对象simplexml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13686881/