java - 使用 JAXB 和 MOXy 将 Java 属性映射到多个 xml 属性

标签 java jaxb eclipselink moxy

我有一个简单的类 CustomQuoteRequest:

public class CustomQuoteRequest {

  private String requestId;

  private String currencyPairCode;

  public String getRequestId() {
    return requestId;
  }

  public void setRequestId(String requestId) {
    this.requestId = requestId;
  }

  public String getCurrencyPairCode() {
    return currencyPairCode;
  }

  public void setCurrencyPairCode(String currencyPairCode) {
    this.currencyPairCode = currencyPairCode;
  }
}

我想将 currencyPairCode 映射到 xml 中的两个不同属性。这是我正在使用的 MOXy 映射文件:

<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/oxm http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_1.xsd"
    >
    <java-types>
        <java-type name="com.anz.fxeasy.domain.model.quote.CustomQuoteRequest"  xml-accessor-type="FIELD">
            <xml-root-element name="FIXML"/>
            <java-attributes>
                <xml-element java-attribute="requestId" xml-path="QuotReq/@ReqId"/>
                <xml-element java-attribute="currencyPairCode" xml-path="QuotReq/QuoteReq/Instrmt/@Sym"></xml-element>
                <xml-element java-attribute="currencyPairCode" xml-path="QuotReq/QuoteReq/Leg/Leg/@Sym"></xml-element>
            </java-attributes>
        </java-type>
    </java-types>

然而,第二个 xml 元素似乎覆盖了前一个。有任何想法吗? 非常感谢

最佳答案

EclipseLink MOXy 2.1.X

在 EclipseLink 2.1.X 中,您可以使用 XML 定制器来完成此操作。您的外部元数据如下所示:

<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/oxm http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_1.xsd"
    >
    <java-types>
        <java-type name="forum78.CustomQuoteRequest"  xml-accessor-type="FIELD" xml-customizer="customizer.CustomQuoteRequestCustomizer">
            <xml-root-element name="FIXML"/>
            <java-attributes>
                <xml-element java-attribute="requestId" xml-path="QuotReq/@ReqId"/>
                <xml-element java-attribute="currencyPairCode" xml-path="QuotReq/QuoteReq/Instrmt/@Sym"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

在定制器中,我们将为 currencyCodePair 属性添加第二个映射。我们需要指明此映射是只写的。 XML 定制器的实现如下所示:

package customizer;

import org.eclipse.persistence.config.DescriptorCustomizer;
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.oxm.mappings.XMLDirectMapping;

public class CustomQuoteRequestCustomizer implements DescriptorCustomizer {

    public void customize(ClassDescriptor descriptor) throws Exception {
        XMLDirectMapping  currencyPairCodeLegMapping = new XMLDirectMapping();
        currencyPairCodeLegMapping.setAttributeName("currencyPairCode");
        currencyPairCodeLegMapping.setXPath("QuotReq/QuoteReq/Leg/Leg/@Sym");
        currencyPairCodeLegMapping.setIsWriteOnly(true);
        descriptor.addMapping(currencyPairCodeLegMapping);

    }

}

EclipseLink MOXy 2.2

在即将发布的 EclipseLink 2.2 版本中,您将能够仅使用外部化元数据来执行此操作:

<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/oxm http://www.eclipse.org/eclipselink/xsds/eclipselink_oxm_2_1.xsd"
    >
    <java-types>
        <java-type name="forum78.CustomQuoteRequest"  xml-accessor-type="FIELD">
            <xml-root-element name="FIXML"/>
            <java-attributes>
                <xml-element java-attribute="requestId" xml-path="QuotReq/@ReqId"/>
                <xml-element java-attribute="currencyPairCode" xml-path="QuotReq/QuoteReq/Instrmt/@Sym"/>
                <xml-element java-attribute="currencyPairCode" xml-path="QuotReq/QuoteReq/Leg/Leg/@Sym" write-only="true"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

以下错误可用于跟踪此支持:

关于java - 使用 JAXB 和 MOXy 将 Java 属性映射到多个 xml 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3964602/

相关文章:

JAVA - 使用套接字和线程接收对象不起作用

java - 使用JAXB_FRAGMENT属性时,是否需要输出XML Declaration?

java - 使用 JAXB 将 JSON 绑定(bind)到 Java 类

java - JPA - 使用可插入/可更新

java - jvisualvm:卡在 “Loading Heap Dump...” 屏幕上

java - JSP 不能在 CQ5 中编译?

mysql - EclipseLink 可以创建 persistence.xml 中指定的数据库吗?

java - 使用 EclipseLink 将行插入到具有唯一标识符列的表中

Java 程序不给出 n>6 值的输出,为什么?

java - 不显示对象标签,仅显示其 JAXB 属性