2

我创建了一个自定义 JiBX 编组器并验证了它的工作原理。它通过执行以下操作来工作:

<binding xmlns:tns="http://foobar.com/foo" direction="output">
  <namespace uri="http://foobar.com/foo" default="elements"/>
  <mapping class="java.util.HashMap" marshaller="com.foobar.Marshaller1"/>
  <mapping name="context" class="com.foobar.Context">
    <structure field="configuration"/>
  </mapping>
</binding>

但是我需要为不同的 HashMaps 创建多个编组器。所以我尝试用这样的抽象映射来引用它:

<binding xmlns:tns="http://foobar.com/foo" direction="output">
  <namespace uri="http://foobar.com/foo" default="elements"/>
  <mapping abstract="true" type-name="configuration" class="java.util.HashMap" marshaller="com.foobar.Marshaller1"/>
  <mapping abstract="true" type-name="overrides" class="java.util.HashMap" marshaller="com.foobar.Marshaller2"/>
  <mapping name="context" class="com.foobar.Context">
    <structure map-as="configuration" field="configuration"/>
    <structure map-as="overrides" field="overrides"/>
  </mapping>
</binding>

但是,这样做时,当我尝试构建绑定时,我会收到以下信息:

Error during code generation for file "E:\project\src\main\jibx\foo.jibx" - this may be due to an error in your binding or classpath, or to an error in the JiBX code

我的猜测是,要么我遗漏了一些我需要实现的东西来启用我的自定义编组器进行抽象映射,要么自定义编组器不支持抽象映射。

我在 JiBX API ( http://jibx.sourceforge.net/api/org/jibx/runtime/IAbstractMarshaller.html ) 中找到了 IAbstractMarshaller 接口,但是我似乎不清楚这是否是我需要实现的文档,以及它是如何工作的。作为示例,我无法找到该接口的实现。

我的问题是,您如何使用自定义编组器进行抽象映射(如果可能的话)?如果它是通过 IAbstractMarshaller 接口完成的,它是如何工作的和/或我应该如何实现它?

4

1 回答 1

2

我不确定 IAbstractMarshaller 接口是否是您正在寻找的;文档有点不清楚。如果您不介意重复,可以直接在结构映射上指定编组器,这应该会得到所需的结果(“配置”和“覆盖”的单独映射):

<binding xmlns:tns="http://foobar.com/foo" direction="output">
  <namespace uri="http://foobar.com/foo" default="elements"/>
  <mapping name="context" class="com.foobar.Context">
    <structure map-as="configuration" field="configuration" marshaller="com.foobar.Marshaller1"/>
    <structure map-as="overrides" field="overrides" marshaller="com.foobar.Marshaller2"/>
  </mapping>
</binding>
于 2011-02-08T15:23:12.880 回答