【问题标题】:Abstract mapping with custom JiBX marshaller使用自定义 JiBX 编组器的抽象映射
【发布时间】:2011-02-08 01:46:47
【问题描述】:

我创建了一个自定义 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>

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

<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 接口完成的,它是如何工作的和/或我应该如何实现它?

【问题讨论】:

    标签: jibx


    【解决方案1】:

    我不确定 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 2013-02-21
      • 1970-01-01
      相关资源
      最近更新 更多