沒有由JAXB生成的@XmlRootElement我正在嘗試從FpML(Finanial產(chǎn)品標(biāo)記語言)4.5版生成Java類。生成了大量代碼,但我無法使用它。嘗試序列化一個(gè)簡(jiǎn)單的文檔,我得到這個(gè):javax.xml.bind.MarshalException
- with linked exception: [com.sun.istack.SAXException2: unable
to marshal type "org.fpml._2008.fpml_4_5.PositionReport"
as an element because it is missing an @XmlRootElement annotation]實(shí)際上沒有類有@XmlRootElement注釋,所以我可以做錯(cuò)什么?我將xjc(JAXB 2.1)指向fpml-main-4-5.xsd,然后包含所有類型。
3 回答

絕地?zé)o雙
TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
這已在上面已經(jīng)鏈接的博客文章的底部提到,但這對(duì)我來說很有用:
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(new JAXBElement<MyClass>(new QName("uri","local"), MyClass.class, myClassInstance), System.out);

眼眸繁星
TA貢獻(xiàn)1873條經(jīng)驗(yàn) 獲得超9個(gè)贊
正如上面的一個(gè)答案中暗示的那樣,如果在XSD中將其類型定義為命名類型,則不會(huì)在根元素上獲得XMLRootElement,因?yàn)樵撁愋涂梢栽赬SD中的其他位置使用。嘗試將其設(shè)為匿名類型,即代替:
<xsd:element name="myRootElement" type="MyRootElementType" /><xsd:complexType name="MyRootElementType">...</xsd:complexType>
你將會(huì)擁有:
<xsd:element name="myRootElement"> <xsd:complexType> ... <xsd:complexType></xsd:element>
添加回答
舉報(bào)
0/150
提交
取消