I have two XSD that I generated through JAXB's xjc command.
http://www.imsglobal.org/xsd/imscp_v1p1.xsd and http://www.mmtech.ru/adlcp_v1p3.xsd
I am trying to use the Item object from the first xsd like
ItemType item = factory.createItemType();
DataType dataType = factory.createDataType();
MapType mapType = factory.createMapType();
dataType.getMap().add(mapType);
item.getAny().add(dataType);
Note that DataType and MapType are both from the two XSD开发者_如何学Go.
I can generate the xml file expect for one problem, it is giving the error
Caused by: com.sun.istack.SAXException2: unable to marshal type "packagename.DataType" as an element because it is missing an @XmlRootElement annotation
So I tried adding the @XmlRootElement to the DataType class and instead of it creating ns2:dataType, it creates the XML below.
<item>
<dataType>
<ns2:map targetID="targetid" writeSharedData="false"/>
</dataType>
</item>
Please help. Thank you
to answer my own question.
We should convert the DataType class to a JAXBElement.
JAXBElement dataTypeElement = factory.createData(dataType);
item.getAny().add(dataTypeElement);
精彩评论