使用简单XML框架继承 [英] Inheritance with Simple XML Framework

查看:135
本文介绍了使用简单XML框架继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用简单XML框架来解析XML文件。

I'm using the Simple XML Framework for parsing XML files.

从服务器收到XML文件,如下所示:

From a server i receive a XML-File what looks like this:

<Objects>
   <Object type="A">
      <name></name>
      <color></color>
   </Object>
   <Object type="B">
      <shape></shape>
      <weight></weight>
   </Object>
<Objects>

我有一个接口(或超类) Object 和两个子类 A B

I have an interface (or superclass) Object and two subclasses A and B

是否可以对此XML文档进行反序列化?

Is it possible to de-serialize this XML-Document?

我在教程中看到过有可能用 class -attribute来区分子类,但不幸的是,这对我来说是不可能的。有没有办法选择框架在type属性的基础上选择正确的子类?

I saw in the Tutorial that there is a possibility to differentiate between subclasses with an class-attribute, but unfortunately this is not possible for me. Is there a way to chose that the framework chooses the right sub-class on the base of the type attribute?

我不能使用另一个框架(如JAXB),因为我使用Android ..

I can't use another Framework (like JAXB) because i use Android..

推荐答案

如果您愿意对XML进行一次小的更改,Simple XML也可以这样做。因此,在您的示例中,您可以将xml更改为如下所示:

Simple XML can do that too if you are willing to make one minor change to your XML. So in your example, you could change that xml to look like this:

<Objects>
   <A>
      <name></name>
      <color></color>
   </A>
   <B>
      <shape></shape>
      <weight></weight>
   </B>
<Objects>

然后你可以拥有以下java:

And then you could have the following java:

@ElementListUnion({
    @ElementList(entry = "A", inline = true, type = A.class),
    @ElementList(entry = "B", inline = true, type = B.class)
}
private List<BaseClass> objects;

这就是真正存在的一切。虽然我不认为它可以基于属性来完成。但我可能错了,你可能想要阅读该文档

And that is all that there really is to it. Though I do not think that it can be done based on an Attribute. I could be wrong though, you might want to read the docs for that one.

这篇关于使用简单XML框架继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆