Jibx:将基类输出集成到扩展类输出中
我有这个类模型:
abstract class A {
int a;
}
class B extends A {
int b;
}
class C extends B {
int c;
}
我想让 jibx 输出这个 XML:
<B b=1 a=0>
<children>
<C c=2 b=1 a=0/>
</children>
</B>
我有这个绑定 xml:
<binding>
<mapping class="A" abstract="true">
<value name="a" field="a" style="attribute" usage="optional"/>
<collection field="children" type="java.util.ArrayList"/>
</mapping>
<mapping name="B" class="B" extends="A">
<value name="b" field="b" style="attribute" usage="optional"/>
<structure map-as="A"/>
</mapping>
<mapping name="C" class="C" extends="B">
<value name="c" field="c" style="attribute" usage="optional"/>
<structure map-as="B"/>
</mapping>
</binding>
但是我不断得到这样的工件:
<C c=2>
<B b=1 a=0>
<children>
...
</children>
</B>
</C>
作为临时解决方案,我已经更改了我的继承结构以让 AbstractB 和 B 扩展 AbstractB C 扩展了 AbstractB,但由于 jibx 而不得不重新设计我的类,这真的让我很恼火。
有人知道如何解决这个问题吗?
编辑: 作为一个额外的问题 - 如何使用 Jibx 来编码/解码 java.util.Map? 我知道它不能在本地完成(很高兴被反驳!)但是你会如何编写 Map 代码(没有字符串)。 请注意,我们没有使用 jibx-extras.jar,因此解决方案不应依赖它。
I have this class model:
abstract class A {
int a;
}
class B extends A {
int b;
}
class C extends B {
int c;
}
And I'd like to get jibx to output this XML:
<B b=1 a=0>
<children>
<C c=2 b=1 a=0/>
</children>
</B>
I have this binding xml:
<binding>
<mapping class="A" abstract="true">
<value name="a" field="a" style="attribute" usage="optional"/>
<collection field="children" type="java.util.ArrayList"/>
</mapping>
<mapping name="B" class="B" extends="A">
<value name="b" field="b" style="attribute" usage="optional"/>
<structure map-as="A"/>
</mapping>
<mapping name="C" class="C" extends="B">
<value name="c" field="c" style="attribute" usage="optional"/>
<structure map-as="B"/>
</mapping>
</binding>
However I keep getting artifacts like this:
<C c=2>
<B b=1 a=0>
<children>
...
</children>
</B>
</C>
As temporary solution I've changed my inheritance structure to have AbstractB and B extends AbstractB and C extends AbstractB, but it really annoys me to have to redesign my class because of jibx.
Anyone knows how to solve this?
Edit:
As a bonus question - how do you use code/decode java.util.Map with Jibx? I know it can't be done natively (would be glad to be disproved!) but what would you do to code Map (no strings). Please note we're not using jibx-extras.jar, so solutions should not rely on it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,恕我直言,将 C 作为 B 的父级(在 XML 意义上)感觉很正常,因为 C 包含 B 的信息(它从中继承)以及它自己的信息但是B不知道那些C特有的信息,对吗?
更清楚地说:B 是鸟,C 是鸡。 鸡是鸟,所以 C 继承了 B,OK。 但在 XML 格式中,会存储 :
或
?
所以从模型的角度来看,对我来说这似乎是合乎逻辑的......
而且我在绑定教程,抱歉。
Actually IMHO it feels kind of normal to get C as parent (in XML sense) of B, because C contains the information of B (from wich it inherits) and its own information aside, but B doesn't know about those C-specific information right ?
To make it clearer: it B is Bird and C is Chicken. Chicken is a Bird, so C inherits form B, OK. But in XML format, would store :
or
?
So on the model point of view, it seems logical to me...
And I didn't find a way of achieving the opposite in the binding tutorial, sorry.