Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Trying to Marshall/Unmarshall xjc generated classes that don't have a @XmlRootElement annotation
Trying to Marshall/Unmarshall xjc generated classes that don't have a @XmlRootElement annotation [message #1060223] Thu, 23 May 2013 15:15 Go to next message
Eric Peters is currently offline Eric PetersFriend
Messages: 1
Registered: May 2013
Junior Member
I'm generating a set of classes from an XSD, in this case I'm also using <jaxb:globalBindings localScoping="toplevel"/> to remove the various nesting of the classes

In a simple example XML there is something like:

After I have a Header class instance I'm trying to marshall it to Xml (marshaller.marshal(o, new StreamResult(sw))) but get the error about the @XmlRootElement not existing:

Caused by: com.sun.istack.SAXException2: unable to marshal type "org.aftermarket.pies.Header" as an element because it is missing an @XmlRootElement annotation
at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError( ~[jaxb-impl-2.2.6.jar:2.2.6]
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot( ~[jaxb-impl-2.2.6.jar:2.2.6]
at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot( ~[jaxb-impl-2.2.6.jar:2.2.6]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write( ~[jaxb-impl-2.2.6.jar:2.2.6]

Is there a way to specify in a custom bindings file classes that should get the XmlRootElement tag (or whatever else it would need to allow me to Marshall/UnMarshall it separately from the

I really want to avoid modifying the generated java files from xjc. I've seen an example of wrapping the marshalling object with a JAXBElement tag, but that seemed like a hacky solution - and I'm not sure how that would fit in with marshalling objects that already have a proper @XmlRootElement/etc


Re: Trying to Marshall/Unmarshall xjc generated classes that don't have a @XmlRootElement annotation [message #1060407 is a reply to message #1060223] Fri, 24 May 2013 13:55 Go to previous message
Blaise Doughan is currently offline Blaise DoughanFriend
Messages: 163
Registered: July 2009
Senior Member

Hi Eric,

I will demonstrate how you can handle this use case using the following XML Schema. The schema contains 2 named complex types: foo and bar. The foo type has 1 corresponding global element, and the bar type has 2.

XML Schema (schema.xsd)

<?xml version="1.0" encoding="UTF-8"?>
    <element name="foo" type="tns:foo"/>
    <complexType name="foo">
        <attribute name="a"/>
    <element name="bar1" type="tns:bar"/>
    <element name="bar2" type="tns:bar"/>
    <complexType name="bar">
        <attribute name="b"/>


Default Generation - xjc schema.xsd

If we generate a Java model from this XML schema we will get Foo and Bar classes and neither of them will be annotated with @XmlRootElement. Instead there will be @XmlElementDecl declarations for each of the global elements in the generated ObjectFactory class.

        ObjectFactory objectFactory = new ObjectFactory();
        Foo foo = new Foo();
        JAXBElement<Foo> fooElement = objectFactory.createFoo(foo);
        Bar bar = new Bar();
        JAXBElement<Bar> bar1Element = objectFactory.createBar1(bar);
        JAXBElement<Bar> bar2Element = objectFactory.createBar2(bar);

Customized Generation - xjc -extension -b binding.xml schema.xsd

We can leverage an XJC extension to create an @XmlRootElement for a class when there is only 1 global element. For the XML schema in this example an @XmlRootElement will be generated on the Foo class, but not on the Bar class. The binding.xml file looks like:

<?xml version="1.0"?>
    xmlns:xjc= ""
    <jxb:bindings schemaLocation="schema.xsd" node="/xs:schema">

For More Information



[Updated on: Fri, 24 May 2013 13:57]

Report message to a moderator

Previous Topic:Call Oracle Function
Next Topic:DML without transaction
Goto Forum:

Current Time: Mon Nov 30 09:50:38 GMT 2015

Powered by FUDForum. Page generated in 0.27496 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software