gpt4 book ai didi

java - 如何让 Jibx 在集合中添加两个实现相同接口(interface)的不同对象?

转载 作者:行者123 更新时间:2023-12-01 16:02:08 26 4
gpt4 key购买 nike

就像标题所说,我有两个实现相同接口(interface)的不同对象,我想将它们添加到同一个集合中。

我的 XML 看起来像:

  <root>
<item-list>
<item>
<type1>
<id>1</id>
</type1>
</item>
<item>
<type2>
<id>2</id>
<other>1</other>
</type2>
</item>
</item-list>
</root>

item 下的标记可以随时为 type1type2。它们具有相似的值,但如您所见,type2 还有另一个字段。

我的界面是:

  public interface Item {

public String getID();
}

我的三门课:

public class MyObj {
private ArrayList<Item> items = new ArrayList<Item>();

public List<Item> getItems() {
return items;
}
}
public class Type1 implements Item{
private String id;

@Override
public String getID() {
return id;
}
}

public class Type2 implements Item {
private String id;
private String other;

@Override
public String getID() {
return id;
}

public String getOther() {
return other;
}
}

我的绑定(bind)如下所示:

<binding direction="input" >

<mapping name="item-list" class="jibx.woe.MyObj" >

<collection field="items" >
<structure name="item" choice="true" ordered="false">

<structure name="type1" map-as="type1"/>

<structure name="type2" map-as="type2"/>

</structure>

</collection>

</mapping>
<mapping class="jibx.woe.Type1" abstract="true" type-name="type1">
<value name="id" field="id"/>
</mapping>

<mapping class="jibx.woe.Type2" abstract="true" type-name="type2">
<value name="id" field="id"/>
<value name="other" field="other"/>
</mapping>

当我运行它时,我收到以下错误:

*** Error during code generation for file 'C:\Projects\IdeaProjects\jibx-woe\src \main\config\woe-binding.xml' -
this may be due to an error in your binding or classpath, or to an error in the JiBX code ***

Internal error: Expected jibx.woe.Type1 on stack, found java.lang.Object full stack:
0: java.lang.Object
1: java.lang.Object
2: org.jibx.runtime.impl.UnmarshallingContext

所以我想我向集合添加一个项目类型,所以现在我的集合部分如下所示:

   <collection field="items" item-type="jibx.woe.Item">
<structure name="item" choice="true" ordered="false" >

<structure name="type1" map-as="type1"/>

<structure name="type2" map-as="type2"/>

</structure>

</collection>

现在错误显示:

  Internal error: Expected jibx.woe.Type1 on stack, found jibx.woe.Item
full stack:
0: jibx.woe.Item
1: jibx.woe.Item
2: org.jibx.runtime.impl.UnmarshallingContext

所以现在我将以下方法添加到 MyObj:

  public void addItem(Object type) {
items.add((Item)type);
}

并将 collection 标记更改为:

  <collection add-method="addItem">

并得到与我第一次尝试相同的错误:

Internal error: Expected jibx.woe.Type1 on stack, found java.lang.Object
full stack:
0: java.lang.Object
1: java.lang.Object
2: org.jibx.runtime.impl.UnmarshallingContext

现在我已经没有主意了。我怎样才能让它发挥作用?

编辑

根据下面 Archie 的建议,我将绑定(bind)更改为:

<mapping name="root" class="jibx.woe.MyObj">

<structure name="item-list" >

<collection field="items">
<structure name="item" /> >

</collection>
</structure>
</mapping>

<mapping name="type1" class="jibx.woe.Type1" >
<value name="id" field="id"/>
</mapping>

<mapping name="type2" class="jibx.woe.Type2" >
<value name="id" field="id"/>
<value name="other" field="other"/>
</mapping>


</binding>

现在绑定(bind)工作没有堆栈错误(万岁!)但是现在当我从列表中获取一个项目时:

    List<Item> items = woe.getItems();
Type1 item = (Type1) items.get(0);

我得到:

    java.lang.ClassCastException: java.lang.Object cannot be cast to jibx.woe.Type1

在我上面的第二行。如果我打印该对象: System.out.println(items.get(0));

它说: java.lang.Object@1be2d65

所以它是一个普通对象,根本不是 Element 或 Type1(或 Type2)!

我确实尝试了 Archie 提出的“扩展”方法,但它没有改变任何东西——我得到了相同的结果。

最佳答案

为 Item 创建抽象映射。然后为 Type1 和 Type2 创建两个具体映射,每个映射都扩展 Item 映射。在您的项目集合中,将每个元素设为一个项目(去掉选择的内容)。

请注意,JiBX 中的“extends”关键字与 Java 类无关,它的意思是“可以替代”。

限制:您必须为 Type1 和 Type2 提供两个不同的 XML 标记名称(例如“type1”和“type2”),以便 JiBX 在解码时可以区分它们。这是你目前所做的事情的问题。例如,当 JiBX 看到一个标签时,它如何知道要实例化哪个 Java 类?

关于java - 如何让 Jibx 在集合中添加两个实现相同接口(interface)的不同对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3587136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com