简体   繁体   中英

JAXB multiple xsd

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.

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);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM