JAXB / MOXY映射问题:属性或字段phoneType必须是属性,因为另一个字段或属性使用XmlValue注释

时间:2012-08-16 14:07:43

标签: jaxb jaxb2 moxy

下面是我的类和OXM映射。如果我希望phoneType是手机下的子元素

,我不知道为什么我会得到以下异常
public class PhoneNumber
{
    public enum PhoneType
    {
        Cell, Home
    };

    private PhoneType phoneType;

    private String type;

    private String number;

    public String getType()
    {
        return type;
    }

    public void setType(final String type)
    {
        this.type = type;
    }

    public String getNumber()
    {
        return number;
    }

    public void setNumber(final String number)
    {
        this.number = number;
    }

    public void setPhoneType(final PhoneType phoneType)
    {
        this.phoneType = phoneType;
    }

    public PhoneType getPhoneType()
    {
        return phoneType;
    }

}

OXM

<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_4.xsd"
   version="2.4"  package-name="blog.bindingfile"  xml-mapping-metadata-complete="true">
   <xml-schema namespace="http://www.example.com/customer" element-form-default="QUALIFIED" />
   <java-types>
      <java-type name="Customer">
         <xml-root-element name="customer"/>
         <xml-type prop-order="lastName firstName address phoneNumbers" />
         <java-attributes>
            <xml-element java-attribute="firstName" name="first-name" />
            <xml-element java-attribute="lastName" name="last-name" />
            <xml-element java-attribute="phoneNumbers" name="phone-number" />
         </java-attributes>
      </java-type>
      <java-type name="BaseCustomer" xml-transient="true">
      <xml-root-element/>
      </java-type>
      <java-type name="PhoneNumber">
         <java-attributes>
            <xml-attribute java-attribute="type" />
            <xml-value java-attribute="number" />
            <xml-element java-attribute="phoneType" name="phone-type"/>
         </java-attributes>
      </java-type>
   </java-types>
</xml-bindings>

异常

Caused by: Exception [EclipseLink-50010] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.JAXBException
Exception Description: The property or field phoneType must be an attribute because another field or property is annotated with XmlValue.
    at org.eclipse.persistence.exceptions.JAXBException.propertyOrFieldShouldBeAnAttribute(JAXBException.java:246)
    at org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor.finalizeProperties(AnnotationsProcessor.java:1011)
    at org.eclipse.persistence.jaxb.compiler.XMLProcessor.processXML(XMLProcessor.java:412)
    at org.eclipse.persistence.jaxb.compiler.Generator.<init>(Generator.java:102)
    at org.eclipse.persistence.jaxb.JAXBContext$ContextPathInput.createContextState(JAXBContext.java:768)
    ... 11 more

1 个答案:

答案 0 :(得分:1)

<强>更新

为了完全理解为什么你会看到异常,我们将看一个格式化的XML片段。问题归结为@XmlValue映射对应的文本片段(第一个,第二个,不是?)

<phone-number type="TYPE">
    <phone-type>Cell</phone-type>
</phone-number>

以下文档涉及混合内容,因此JAXB会让您明确地将其调用。通常混合与@XmlAnyElement一起使用,以便获得所有元素和文本的列表,以便维护订单。

<phone-number type="TYPE">
    555-1111
    <phone-type>Cell</phone-type>
</phone-number>

由于您的OXM文件中存在以下内容,您将收到异常:

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

如异常所述,如果类中的另一个字段/属性与@XmlElement映射

,则不允许使用@XmlValue映射字段/属性

您需要做什么

您确实在尝试使用混合内容映射元素。您可以使用以下映射文件执行此操作:

<java-type name="PhoneNumber">
    <java-attributes>
        <xml-attribute java-attribute="type" />
        <xml-any-element java-attribute="number" xml-mixed="true"/>
        <xml-element java-attribute="phoneType" name="phone-type" />
    </java-attributes>
</java-type>

完整示例

oxm.xml

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

演示

package forum11988974;

import java.io.File;
import java.util.*;
import javax.xml.bind.*;
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, "forum11988974/oxm.xml");
        JAXBContext jc = JAXBContext.newInstance(new Class[] {PhoneNumber.class}, properties);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum11988974/input.xml");
        PhoneNumber phoneNumber = (PhoneNumber) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.marshal(phoneNumber, System.out);
    }

}

input.xml中/输出

<?xml version="1.0" encoding="UTF-8"?>
<phone-number type="TYPE">555-1111<phone-type>Cell</phone-type></phone-number>