I have to generate a xml element that can have as value any "primitive type" (xsd:string, xsd:boolean, etc). Examples:
<field xsi:type="xsd:string" name="aString">String Value</field>
<field xsi:type="xsd:date" name="aDate">2011-10-21</field>
...
So, I tried two implementations:
public class Field {
@XmlAttribute
private String name;
@XmlValue
Object value;
}
and ...
public class Field<T> {
@XmlAttribute
private String name;
@XmlValue
T value;
}
I'm testing this with:
Marshaller marshaller = JAXBContext.newInstance(Field.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
Field field = new Field();
field.name = "name";
field.value = "value";
ByteArrayOutputStream stream = new ByteArrayOutputStream();
marshaller.marshal(field, new PrintWriter(stream));
System.out.println(stream);
But I'm getting this NullPointerException
when I try to instantiate the JAXBContext.
java.lang.NullPointerException
at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:165)
at com.sun.xml.bind.v2.runtime.property.ValueProperty.<init>(ValueProperty.java:77)
at com.sun.xml.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:106)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:179)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:515)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:330)
at
The idea is allow schema validation for the field element (It is define in a schema must its type will be set in each instance). So, even is this a Bug (or not) ... how JAXB will put the correct xsi:type
to this field instance? I'm missing a concept here?
I know that maybe the problem is the usage of the @XmlValue
because of this restrictions (from the javadoc):
- At most one field or property can be annotated with the @XmlValue annotation.
- @XmlValue can be used with the following annotations: XmlList. However this is redundant since XmlList maps a type to a simple schema type that derives by list just as XmlValue would.
- If the type of the field or property is a collection type, then the collection item type must map to a simple schema type.
- If the type of the field or property is not a collection type, then the type must map to a XML Schema simple type.
... because an Object
or a generic T
is not necessarily a XML Schema simple type, this approach seems not to be the correct one ...
Thanks in advance ...
I have confirmed the issue you are seeing in both the reference and EclipseLink JAXB (MOXy) implementations of JAXB. The problem you are seeing is due to use of @XmlValue
. If the value
property was mapped as an @XmlElement
you would see the xsi:type
attribute appear as expected.
I have entered the following bug to track this issue in EclipseLink JAXB (MOXy):
Depending upon what your domain model looks like you may be interested in the @XmlPath
extension from EclipseLink JAXB (MOXy):
UPDATE
This issue has now been fixed in the EclipseLink 2.3.3 and EclipseLink 2.4.0. The fix is available in these streams starting March 17, 2012 and can be obtained from:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.