JAXB - 如何根据XML值设置XML元素的xsi:类型?

时间:2022-11-19 14:48:52

I have to generate a xml element that can have as value any "primitive type" (xsd:string, xsd:boolean, etc). Examples:

我必须生成一个xml元素,它可以具有任何“基本类型”(xsd:string,xsd:boolean等)作为值。例子:

<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.

但是当我尝试实例化JAXBContext时,我得到了这个NullPointerException。

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?

这个想法是允许字段元素的模式验证(它在模式中定义必须在每个实例中设置其类型)。所以,即使这是一个Bug(或不是)...... JAXB如何将正确的xsi:type放到这个字段实例中?我在这里错过了一个概念?

I know that maybe the problem is the usage of the @XmlValue because of this restrictions (from the javadoc):

我知道问题可能是@XmlValue的使用,因为这个限制(来自javadoc):

  • At most one field or property can be annotated with the @XmlValue annotation.
  • 最多可以使用@XmlValue批注对一个字段或属性进行批注。

  • @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.
  • @XmlValue可以与以下注释一起使用:XmlList。然而,这是多余的,因为XmlList将类型映射到一个简单的模式类型,该模式类型按照XmlValue的方式按列表派生。

  • 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.
  • 如果字段或属性的类型不是集合类型,则该类型必须映射到XML Schema简单类型。

... because an Object or a generic T is not necessarily a XML Schema simple type, this approach seems not to be the correct one ...

...因为Object或泛型T不一定是XML Schema简单类型,这种方法似乎不是正确的...

Thanks in advance ...

提前致谢 ...

1 个解决方案

#1


2  

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.

我已经确认了您在JAXB的引用和EclipseLink JAXB(MOXy)实现中看到的问题。您看到的问题是由于使用了@XmlValue。如果value属性被映射为@XmlElement,您会看到xsi:type属性按预期显示。

I have entered the following bug to track this issue in EclipseLink JAXB (MOXy):

我在EclipseLink JAXB(MOXy)中输入了以下错误来跟踪此问题:

Depending upon what your domain model looks like you may be interested in the @XmlPath extension from EclipseLink JAXB (MOXy):

根据您的域模型的样子,您可能对EclipseLink JAXB(MOXy)中的@XmlPath扩展感兴趣:

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:

此问题现已在EclipseLink 2.3.3和EclipseLink 2.4.0中得到修复。从2012年3月17日开始,可以在这些流中使用此修复程序,可以从以下位置获取:

#1


2  

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.

我已经确认了您在JAXB的引用和EclipseLink JAXB(MOXy)实现中看到的问题。您看到的问题是由于使用了@XmlValue。如果value属性被映射为@XmlElement,您会看到xsi:type属性按预期显示。

I have entered the following bug to track this issue in EclipseLink JAXB (MOXy):

我在EclipseLink JAXB(MOXy)中输入了以下错误来跟踪此问题:

Depending upon what your domain model looks like you may be interested in the @XmlPath extension from EclipseLink JAXB (MOXy):

根据您的域模型的样子,您可能对EclipseLink JAXB(MOXy)中的@XmlPath扩展感兴趣:

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:

此问题现已在EclipseLink 2.3.3和EclipseLink 2.4.0中得到修复。从2012年3月17日开始,可以在这些流中使用此修复程序,可以从以下位置获取: