2012-08-16 54 views
1

爲什麼我無法使用外部映射覆蓋「type」屬性爲非瞬態,如下所示?當我序列化時,我沒有看到「type」元素。在Moxy中使用外部映射文件覆蓋@XmlTransient行爲

public class PhoneNumber 
{ 
    private String type; 

    @XmlTransient 
    public String getType() 
    { 
     return type; 
    } 

    //other properties 
} 

我使用「xml-attribute」指定了「type」,希望這將優先於註釋,但它不起作用。

<java-type name="PhoneNumber"> 
     <java-attributes> 
      <xml-attribute java-attribute="type" /> 
      <xml-value java-attribute="number" /> 
     </java-attributes> 
</java-type> 

回答

1

您似乎遇到了錯誤。您可以通過下面的鏈接跟蹤在這個問題上我們的進展:

替代方法

你可以指定字段訪問應該用於PhoneNumber類。

<java-type name="PhoneNumber" xml-accessor-type="FIELD"> 
     <java-attributes> 
      <xml-attribute java-attribute="type" /> 
      <xml-value java-attribute="number"/> 
     </java-attributes> 
    </java-type> 

FULL例

******中國

package forum11991936; 

import javax.xml.bind.annotation.XmlTransient; 

public class PhoneNumber { 
    private String type; 
    private String number; 

    @XmlTransient 
    public String getType() { 
     return type; 
    } 
    public void setType(String type) { 
     this.type = type; 
    } 
    public String getNumber() { 
     return number; 
    } 
    public void setNumber(String number) { 
     this.number = number; 
    } 

} 

oxm.xml

<?xml version="1.0" encoding="UTF-8"?> 
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" 
    package-name="forum11991936"> 
    <java-types> 
     <java-type name="PhoneNumber" xml-accessor-type="FIELD"> 
      <xml-root-element name="phone-number"/> 
      <java-attributes> 
       <xml-attribute java-attribute="type" /> 
       <xml-value java-attribute="number"/> 
      </java-attributes> 
     </java-type> 
    </java-types> 
</xml-bindings> 

個jaxb.properties

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory 

演示

package forum11991936; 

import java.util.*; 
import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Marshaller; 

import org.eclipse.persistence.jaxb.JAXBContextProperties; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     Map<String, Object> properties = new HashMap<String, Object>(); 
     properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum11991936/oxm.xml"); 
     JAXBContext jc = JAXBContext.newInstance(new Class[] {PhoneNumber.class}, properties); 

     PhoneNumber pn = new PhoneNumber(); 
     pn.setType("cell"); 
     pn.setNumber("555-1111"); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(pn, System.out); 
    } 
} 

輸出

<?xml version="1.0" encoding="UTF-8"?> 
<phone-number type="cell">555-1111</phone-number> 
+0

THX。你也可以看看這個http://stackoverflow.com/questions/11993667/reusable-complextype-and-xml-registry-xml-element-decl-usage。我甚至不確定如何解決這個問題。 – 2012-08-16 21:06:58

+0

@Pangea - 我已經添加了一個答案:http://stackoverflow.com/a/11995509/383861 – 2012-08-16 21:14:10