我正在使用Jaxb来生成Java类。我的架构定义了以下元素:如何使用由Jaxb生成的ObjectFactory?
<xs:complexType name="AutomobileType" abstract="true">
<xs:sequence>
<xs:element name="Color" type="core:ColorName"/>
<xs:element name="Weight" type="core:PoundsWeightType"/>
<xs:element name="Fuel" type="Fuel"/>
<xs:element name="NumDoors" type="xs:nonNegativeInteger"/>
<xs:element name="NumCylinders">
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="12"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:element name="Automobile" type="AutomobileType"/>
正如你所看到的,我只有一个单元叫汽车。
Jaxb创建类和一个ObjectFactory,我用它来创建实例汽车。这令我感到困惑是创建汽车的实例方法的事情如下:
public JAXBElement<AutomobileType> createAutomobile(AutomobileType value)
为什么createAutomobile方法有一个说法?我如何使用这种方法?
我试过如下:
ObjectFactory objectFactory = new ObjectFactory();
objectFactory.createAutomobile(new Automobile());
但这并不编译,因为汽车类是抽象的,所以我不能创建一个实例。
我将Automobile类更改为Not be Abstract,现在一切正常。但是,当我尝试编组汽车实例时,出现以下错误:无法编组类型“com.cookmv.dealership.vehicles.schemas.automobile.AutomobileType”作为元素,因为它缺少@XmlRootElement注释] –