2010-10-19 4 views
2

У меня есть простой класс CustomQuoteRequest:Картирования свойства Java для нескольких XML атрибутов с использованием JAXB и Moxy

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, кажется, переопределяет предыдущий. Есть идеи? Большое спасибо

ответ

2

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> 

следующая ошибка может быть использована для отслеживания этой поддержки:

+0

Отлично, спасибо большое за это! – Sergio