I have a complex xml schema that is part of the international standard, so I cannot modify the schema files, and I used Maven to create JAXB objects from them. When I try to set data in my JAXB objects and then the marshal of the root element, I get the missing @XmlRootElement error for several complex types. My question is: how can I solve this? Environment:
- JAXB 2.2.2
- Maven 3.0.3
- Eclipse juno
- Java 1.6
- Windows 7 64 bit
I researched the website and this website, but could not find an answer to my problem, including this post: http://weblogs.java.net/blog/kohsuke/archive/2006/03/why_does_jaxb_p.html
and this stack overflow question: JAX-WS MarshalException with JAX-B custom bindings: failed to marshal the type "java.lang.String" as an element because the @XmlRootElement annotation is missing
I tried adding annotation to intruder classes in my binding file, but adding annotation to the class, as well as the getContent () method, and the class will not compile. I donβt think that I will have to add this annotation anyway, since this is a complex type that is a component of the root, in fact it is deeply embedded in the tree. I read that you can add the <xjc:simple/> to the global anchor element, but I have added several attributes to the global anchor element and it will not compile when I do this.
Can someone help me point me in the right direction, maybe this is a mistake in how I create JAXB objects? I went through it in the debugger, and it seems that when the ArrayReferenceNodeProperty class from the com.sun.xml.bind.v2.runtime.property package tries to serialize the body of the list, and when it receives JaxBeanInfo for these classes, t the dom and jaxbType handler set is not object, therefore, he believes that it should be the root.
I do not use this in the web service, and in addition to tweets in the binding file, I did not create any custom classes or bindings to resolve name conflicts.
Here is the top-level schema file:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified"> <xs:annotation> <xs:documentation> ***************************************************************************** * * * S1000D Issue 3 * * XML Schema * * * * Release Version 3.0 * * Date: 2007-07-31 * * * * * * The following attribute definition on the root element may be used to * * call this set of declarations:- * * * * xsi:noNamespaceSchemaLocation= * * "http://www.s1000d.org/S1000D_3-0/xml_schema_master/dm/procedSchema.xsd" * ***************************************************************************** </xs:documentation> </xs:annotation> <xs:redefine schemaLocation="elementGroups.xsd"> <xs:group name="APPLIC"> <xs:choice> <xs:sequence> <xs:element ref="displaytext"/> <xs:choice minOccurs="0"> <xs:element ref="assert"/> <xs:element ref="evaluate"/> </xs:choice> </xs:sequence> <xs:choice> <xs:element ref="assert"/> <xs:element ref="evaluate"/> </xs:choice> </xs:choice> </xs:group> <xs:group name="NPAR"> <xs:sequence> <xs:group ref="WC"/> <xs:group ref="NPFFT" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> </xs:group> <xs:group name="NPFFT"> <xs:choice> <xs:group ref="NP"/> <xs:group ref="FFT"/> </xs:choice> </xs:group> <xs:group name="paracon"> <xs:choice> <xs:group ref="text"/> <xs:group ref="list"/> </xs:choice> </xs:group> <xs:group name="text"> <xs:choice> <xs:element ref="ein"/> <xs:element ref="cb"/> <xs:element ref="parasigdata"/> <xs:element ref="quantity"/> <xs:element ref="xref"/> <xs:element ref="indxflag"/> <xs:element ref="change"/> <xs:element ref="emphasis"/> <xs:element ref="symbol"/> <xs:element ref="subscrpt"/> <xs:element ref="supscrpt"/> <xs:element ref="refdm"/> <xs:element ref="reftp"/> <xs:element ref="ftnote"/> <xs:element ref="ftnref"/> <xs:element ref="acronym"/> <xs:element ref="acroterm"/> <xs:element ref="capgrp"/> </xs:choice> </xs:group> <xs:group name="TEXT-NOREFS"> <xs:choice> <xs:element ref="xref"/> <xs:element ref="indxflag"/> <xs:element ref="symbol"/> <xs:element ref="subscrpt"/> <xs:element ref="supscrpt"/> <xs:element ref="ftnref"/> <xs:element ref="acronym"/> <xs:element ref="acroterm"/> </xs:choice> </xs:group> <xs:group name="STEP1"> <xs:sequence> <xs:element ref="step1"/> </xs:sequence> </xs:group> <xs:group name="STEP2"> <xs:sequence> <xs:element ref="step2"/> </xs:sequence> </xs:group> <xs:group name="STEP3"> <xs:sequence> <xs:element ref="step3"/> </xs:sequence> </xs:group> <xs:group name="STEP4"> <xs:sequence> <xs:element ref="step4"/> </xs:sequence> </xs:group> <xs:group name="STEP5"> <xs:sequence> <xs:element ref="step5"/> </xs:sequence> </xs:group> <xs:group name="STEP6"> <xs:sequence> <xs:element ref="step6"/> </xs:sequence> </xs:group> <xs:group name="STEP7"> <xs:sequence> <xs:element ref="step7"/> </xs:sequence> </xs:group> <xs:group name="STEP8"> <xs:sequence> <xs:element ref="step8"/> </xs:sequence> </xs:group> </xs:redefine> <xs:redefine schemaLocation="complexTypes.xsd"> <xs:complexType name="contentType"> <xs:complexContent> <xs:restriction base="contentType"> <xs:sequence> <xs:element ref="refs" minOccurs="0"/> <xs:element ref="proced"/> </xs:sequence> <xs:attribute ref="id"/> </xs:restriction> </xs:complexContent> </xs:complexType> </xs:redefine> <xs:element name="dmodule" type="dmoduleType"/>
For xrefType I set only two attributes, and for refdmType I set only one complex type, which is part of the ALLDMC group. Code for two complex types:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" elementFormDefault="qualified" attributeFormDefault="qualified"> <xs:import namespace="http://www.w3.org/1999/02/22-rdf-syntax-ns#" schemaLocation="rdf.xsd"/> <xs:import namespace="http://www.w3.org/1999/xlink" schemaLocation="xlink.xsd"/> <xs:include schemaLocation="attributeGroups.xsd"/> <xs:include schemaLocation="simpleElements.xsd"/> <xs:include schemaLocation="complexElements.xsd"/> <xs:include schemaLocation="elementGroups.xsd"/> <xs:complexType name="refdmType"> <xs:choice> <xs:sequence> <xs:element ref="applic" minOccurs="0"/> <xs:element ref="dmcextension" minOccurs="0"/> <xs:group ref="ALLDMC"/> <xs:element ref="issno" minOccurs="0"/> <xs:element ref="dmtitle" minOccurs="0"/> <xs:element ref="language" minOccurs="0"/> </xs:sequence> <xs:group ref="xlink:XLINKEXT" minOccurs="0" maxOccurs="unbounded"/> </xs:choice> <xs:attribute ref="target"/> <xs:attribute ref="refapplic"/> <xs:attributeGroup ref="bodyatt"/> <xs:attributeGroup ref="cntlcontent"/> <xs:attributeGroup ref="xlink:XLINKATT"/> </xs:complexType> <xs:complexType name="xrefType" mixed="true"> <xs:sequence> <xs:element ref="applic" minOccurs="0"/> <xs:group ref="SPARCON" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute ref="xrefid"/> <xs:attribute ref="xidtype"/> <xs:attribute ref="target"/> <xs:attribute ref="destitle"/> <xs:attribute ref="pretext"/> <xs:attribute ref="posttext"/> <xs:attribute ref="refapplic"/> <xs:attributeGroup ref="cntlcontent"/> <xs:attributeGroup ref="xlink:XLINKATT3"/> </xs:complexType> </xs:schema>
And here is the bulk of the generated JAXB classes. I removed most getters and setters to save space: RefdmType:
@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "refdmType", propOrder = { "applicAndDmcextensionAndAge" }) public class RefdmType implements Serializable { private final static long serialVersionUID = 1L; @XmlElements({ @XmlElement(name = "applic", type = ApplicType.class), @XmlElement(name = "dmcextension", type = DmcextensionType.class), @XmlElement(name = "age", type = AgeType.class), @XmlElement(name = "avee", type = AveeType.class), @XmlElement(name = "issno", type = IssnoType.class), @XmlElement(name = "dmtitle", type = DmtitleType.class), @XmlElement(name = "language", type = LanguageType.class), @XmlElement(name = "resource", namespace = "http://www.w3.org/1999/xlink", type = Resource.class), @XmlElement(name = "locator", namespace = "http://www.w3.org/1999/xlink", type = Locator.class), @XmlElement(name = "arc", namespace = "http://www.w3.org/1999/xlink", type = Arc.class) }) protected List<Serializable> applicAndDmcextensionAndAge; @XmlAttribute(name = "target") protected String target; @XmlAttribute(name = "refapplic") @XmlIDREF @XmlSchemaType(name = "IDREF") protected Object refapplic; @XmlAttribute(name = "type", namespace = "http://www.w3.org/1999/xlink") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) protected String type; @XmlAttribute(name = "href", namespace = "http://www.w3.org/1999/xlink") protected String href; @XmlAttribute(name = "title", namespace = "http://www.w3.org/1999/xlink") protected String title; @XmlAttribute(name = "show", namespace = "http://www.w3.org/1999/xlink") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) protected String show; @XmlAttribute(name = "actuate", namespace = "http://www.w3.org/1999/xlink") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) protected String actuate; @XmlAttribute(name = "authname") protected String authname; @XmlAttribute(name = "authdoc") protected String authdoc; @XmlAttribute(name = "id") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlID @XmlSchemaType(name = "ID") protected String id; @XmlAttribute(name = "level") @XmlSchemaType(name = "nonNegativeInteger") protected BigInteger level; @XmlAttribute(name = "mark") @XmlSchemaType(name = "nonNegativeInteger") protected BigInteger mark; @XmlAttribute(name = "change") protected ChangeType change; @XmlAttribute(name = "rfc") protected String rfc; public List<Serializable> getApplicAndDmcextensionAndAge() { if (applicAndDmcextensionAndAge == null) { applicAndDmcextensionAndAge = new ArrayList<Serializable>(); } return this.applicAndDmcextensionAndAge; } }
XrefType:
@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "xrefType", propOrder = { "content" }) public class XrefType implements Serializable { private final static long serialVersionUID = 1L; @XmlElementRefs({ @XmlElementRef(name = "supscrpt", type = JAXBElement.class), @XmlElementRef(name = "subscrpt", type = JAXBElement.class), @XmlElementRef(name = "applic", type = JAXBElement.class) }) @XmlMixed protected List<Serializable> content; @XmlAttribute(name = "xrefid") @XmlJavaTypeAdapter(Adapter5 .class) @XmlSchemaType(name = "IDREF") protected String xrefid; @XmlAttribute(name = "xidtype") protected XidtypeType xidtype; @XmlAttribute(name = "target") protected String target; @XmlAttribute(name = "destitle") protected String destitle; @XmlAttribute(name = "pretext") protected String pretext; @XmlAttribute(name = "posttext") protected String posttext; @XmlAttribute(name = "refapplic") @XmlIDREF @XmlSchemaType(name = "IDREF") protected Object refapplic; @XmlAttribute(name = "authname") protected String authname; @XmlAttribute(name = "authdoc") protected String authdoc; @XmlAttribute(name = "type", namespace = "http://www.w3.org/1999/xlink") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) protected String type; @XmlAttribute(name = "href", namespace = "http://www.w3.org/1999/xlink") protected String href; @XmlAttribute(name = "title", namespace = "http://www.w3.org/1999/xlink") protected String title; @XmlAttribute(name = "show", namespace = "http://www.w3.org/1999/xlink") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) protected String show; @XmlAttribute(name = "actuate", namespace = "http://www.w3.org/1999/xlink") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) protected String actuate; public List<Serializable> getContent() { if (content == null) { content = new ArrayList<Serializable>(); } return this.content; } }
And finally, here is my custom binding file:
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:annox="http://annox.dev.java.net" xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"> <jxb:globalBindings typesafeEnumMemberName="generateName" typesafeEnumMaxMembers="1500" collectionType="java.util.ArrayList" generateValueClass="true" choiceContentProperty="true"> <xjc:serializable uid="1" /> </jxb:globalBindings> <jxb:bindings schemaLocation="simpleTypes.xsd" node="/xs:schema"> <jxb:bindings node="//xs:simpleType[@name='YEAR']"> <jxb:javaType name="java.lang.Integer" /> </jxb:bindings> <jxb:bindings node="//xs:simpleType[@name='DAY']"> <jxb:javaType name="java.lang.String" /> </jxb:bindings> <jxb:bindings node="//xs:simpleType[@name='MONTH']"> <jxb:javaType name="java.lang.String" /> </jxb:bindings> <jxb:bindings node="//xs:simpleType[@name='issnovalueType']"> <jxb:javaType name="java.lang.String" /> </jxb:bindings> </jxb:bindings> <jxb:bindings schemaLocation="attributes.xsd" node="/xs:schema"> <jxb:bindings node="xs:attribute[@name='inwork']"> <jxb:property> <jxb:baseType> <jxb:javaType name="java.lang.String" /> </jxb:baseType> </jxb:property> </jxb:bindings> <jxb:bindings node="//xs:attribute[@name='xrefid']"> <jxb:property> <jxb:baseType> <jxb:javaType name="java.lang.String" /> </jxb:baseType> </jxb:property> </jxb:bindings> </jxb:bindings> <jxb:bindings schemaLocation="complexTypes.xsd" node="/xs:schema"> <jxb:bindings node="xs:complexType[@name='dmoduleType']"> <annox:annotate> <annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="dmodule" /> </annox:annotate> </jxb:bindings> <jxb:bindings node="//xs:complexType[@name='skillType']"> <jxb:class name="SkillTypeComplex" /> </jxb:bindings> </jxb:bindings> <jxb:bindings schemaLocation="elementGroups.xsd" node="//xs:group[@name='APPLIC']"> <jxb:bindings node=".//xs:choice//xs:sequence"> <jxb:bindings node=".//xs:choice"> <jxb:property name="assertEvalChoice" /> </jxb:bindings> </jxb:bindings> </jxb:bindings> <jxb:bindings schemaLocation="dc.xsd" node="/xs:schema"> <jxb:bindings node="//xs:element[@name='language']"> <jxb:class name="DcLanguage" /> </jxb:bindings> <jxb:bindings node="//xs:element[@name='subject']"> <jxb:class name="DcSubject" /> </jxb:bindings> <jxb:bindings node="//xs:element[@name='title']"> <jxb:class name="DcTitle" /> </jxb:bindings> </jxb:bindings> </jxb:bindings>
A high understanding of why this will happen and recommendations for fixing it will be appreciated. I have been using JAXB for only a couple of months and not an expert. Thanks.
Here is the error I see:
javax.xml.bind.MarshalException - with a related exception: [com.sun.istack.SAXException2: it is not possible to marshal the type "com.synesis7.s7exporter.domain.s1000d.dm.common.XrefType" as an element because the @XmlRootElement annotation is missing ] at com.sun.xml.bind.v2.runtime.MarshallerImpl.write (MarshallerImpl.java:323) at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal (MarshallerImpl.java:249) in javax. xml.bind.helpers.AbstractMarshallerImpl.marshal (AbstractMarshallerImpl.java:96) on com.synesis7.s7exporter.client.s1000d.procedural.S7ProceduralRunnable.writeToXml (S7ProceduralRunnable.java:142) on com.synesis7.s1expdcl. procedural.S7ProceduralRunnable.createProceduralDm (S7ProceduralRunnable.java:118) on com.synesis7.s7exporter.client.s1000d.procedural.S7ProceduralRunnable.run (S7ProceduralRunnable.java:93) on com.synesis7Exporter.S ler.processProceduralDms (S7ExporterController.java:235) at com.synesis7.s7exporter.S7ExporterController.exportSchema (S7ExporterController.java:118) at com.synesis7.s7exporter.S7exporter.initializeExporter (S7exporter.javaesis) .S7exporter.main (S7exporter.java:38) Caused by: com.sun.istack.SAXException2: it is not possible to output a marshal like "com.synesis7.s7exporter.domain.s1000d.dm.common.XrefType" as there is no annotation @ XmlRootElement at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError (XMLSerializer.java:252) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot (ClassBeanInfoImpl.java:324) at com. sun.xml.bind.v2.runtime.property.ArrayReferenceNodeProperty.serializeListBody (ArrayReferenceNodeProperty.java:118) at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody (ArrayERProperty.java:159) .sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody (ClassBeanInfoImpl.javahaps44) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType (XMLSerializer.java:700) at com.sun.xml .bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem (ArrayElementNodeProperty.java:69) at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody (ArrayElementProperty.java17217 .bind.v2.runtime.property.ArrayERProperty.serializeBody (ArrayERProperty.java:159) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody (ClassBeanInfoImpl.javahaps44) at com.sun.xml.bind .v2.runtime.XMLSerializer.childAsXsiType (XMLSerializer.java:700) at com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem (ArrayElementNodeProperty.java:69) at com.sun.vml.bind .runtime.property.ArrayElementProperty.serializeListBody (ArrayElementProperty.java:172) at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody (ArrayERP roperty.java:159) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody (ClassBeanInfoImpl.javahaps44) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType (XMLSerializer.java: 700) at com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem (ArrayElementNodeProperty.java:69) at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody (ArrayEle JavaProperty.mentProPromenttypromentmentPromentty 172) at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody (ArrayERProperty.java:159) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody (ClassBeanInfoImpl.javahaps44) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType (XMLSerializer.java:700) at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody (SingleElementNodeProperty.java:156) com .sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody (ClassBeanInfoImpl.javahaps44) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType (XMLSer ializer.java:700) at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody (SingleElementNodeProperty.java:156) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody (ClassBean java: 344) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody (ClassBeanInfoImpl.javahaps38) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType (XMLSerializer.java:700) at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody (SingleElementNodeProperty.java:156) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody (ClassBeanInfoImpl.javaictionary44) .sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType (XMLSerializer.java:700) at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody (SingleElementNodeProperty.java:156) com .xml.bind.v2.runtime.ElementBeanInfoImpl $ 1.serializeBody (ElementBeanInfoImpl.java:161) at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl $ 1 .serializeBody (ElementBeanInfoImpl.java:193) at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeBody (ElementBeanInfoImpl.java:321) at com.sun.xml.bind.v2.runtime.ElementBeanfoplplanmentfolementpllementpllementfolementplle .java: 328) at com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl.serializeRoot (ElementBeanInfoImpl.java:76) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot (XMLSerializer.java:498 ) on com.sun.xml.bind.v2.runtime.MarshallerImpl.write (MarshallerImpl.javahaps20) ... 9 more