下面是我的类和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发布于 2012-08-16 14:52:05
更新
为了充分理解为什么要看到异常,我们将查看一个格式化的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>Demo
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/Output
<?xml version="1.0" encoding="UTF-8"?>
<phone-number type="TYPE">555-1111<phone-type>Cell</phone-type></phone-number>https://stackoverflow.com/questions/11988974
复制相似问题