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

发布于 2024-09-28 09:13:43 字数 1583 浏览 2 评论 0原文

我有一个简单的类 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 元素似乎覆盖了前一个。有什么想法吗? 多谢

I have a simple class 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;
  }
}

I would like to map currencyPairCode to two different attributes in the xml. This is the MOXy mapping file I am using:

<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>

However the second xml-element seems to override the previous one. Any ideas?
Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜妞爱困 2024-10-05 09:13:43

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>

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

EclipseLink MOXy 2.1.X

In EclipseLink 2.1.X you can use an XML Customizer to accomplish this. Your external metadata would look like the following:

<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>

In the customizer we'll add a second mapping for the currencyCodePair property. We will need to indicate that this mapping is write only. The implementation of the XML customizer would look like the following:

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

In the upcoming EclipseLink 2.2 release you will be able to do this using just the externalized metadata:

<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>

The following bug can be used to track this support:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文