代码之家  ›  专栏  ›  技术社区  ›  Sergio

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

  •  2
  • Sergio  · 技术社区  · 14 年前

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

    1 回复  |  直到 14 年前
        1
  •  2
  •   bdoughan    14 年前

    日食线MOXy 2.1.X

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