Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To tie together what others have already stated or hinted at, the rules by which JAXB XJC decides whether or not to put the <code>@XmlRootElement</code> annotation on a generated class are non trivial (<a href="http://web.archive.org/web/20160117210900/https://community.oracle.com/blogs/kohsuke/2006/03/03/why-does-jaxb-put-xmlrootelement-sometimes-not-always" rel="noreferrer">see this article</a>).</p> <p><code>@XmlRootElement</code> exists because the JAXB runtime requires certain information in order to marshal/unmarshal a given object, specifically the XML element name and namespace. You can't just pass any old object to the Marshaller. <code>@XmlRootElement</code> provides this information.</p> <p>The annotation is just a convenience, however - JAXB does not require it. The alternative to is to use <code>JAXBElement</code> wrapper objects, which provide the same information as <code>@XmlRootElement</code>, but in the form of an object, rather than an annotation. </p> <p>However, <code>JAXBElement</code> objects are awkward to construct, since you need to know the XML element name and namespace, which business logic usually doesn't. </p> <p>Thankfully, when XJC generates a class model, it also generates a class called <code>ObjectFactory</code>. This is partly there for backwards compatibility with JAXB v1, but it's also there as a place for XJC to put generated factory methods which create <code>JAXBElement</code> wrappers around your own objects. It handles the XML name and namespace for you, so you don't need to worry about it. You just need to look through the <code>ObjectFactory</code> methods (and for large schema, there can be hundreds of them) to find the one you need.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload