Add namespace prefix to only root elements?
<p>I have a XSD from which I generate my Java classes using <code>xjc</code>. </p>

<p><strong>XSD:</strong></p>

<pre><code>&lt;xs:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/xml/protocols"
    xmlns:abc="http://example.com/xml/protocols" elementFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;

    &lt;xs:element name="student"&gt;
        &lt;xs:complexType&gt;
            &lt;xs:complexContent&gt;
                &lt;!-- Inherit common attributes from Regular Type --&gt;
                &lt;xs:extension base="abc:regular"&gt;
                    &lt;xs:sequence&gt;
                        &lt;!-- Resource Specific Attributes --&gt;
                        &lt;xs:element name="name" type="abc:name" minOccurs="0" /&gt;
                        &lt;xs:element name="URI" type="abc:listOfURIs" /&gt;

                        &lt;xs:element name="content" type="abc:content" /&gt;


            &lt;/xs:complexContent&gt;
        &lt;/xs:complexType&gt;
    &lt;/xs:element&gt;
</code></pre>

<p><strong>Student.java:</strong></p>

<pre><code>@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "eventNotificationCriteria",
    "notificationURI",
    "notificationContentType"
})
@XmlRootElement(name = "student")
public class Student
    extends Regular
{

    protected Name name;
    @XmlList
    @XmlElement(required = true)
    protected List&lt;String&gt; URI;

    @XmlElement(required = true)
    protected BigInteger content;
.........
}   
</code></pre>

<p><strong>package-info.java</strong></p>

<pre><code>@XmlSchema(
    elementFormDefault=XmlNsForm.QUALIFIED,
    xmlns={@XmlNs(prefix="abc", 
                  namespaceURI="http://example.com/xml/protocols")}
)
package package.com;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
</code></pre>

<p>I marshall this object to get:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
&lt;abc:student xmlns:abc="http://www.example.com/xml/protocols"&gt;
    &lt;name&gt;
        &lt;firstname&gt;Jack&lt;/firstname&gt;
    &lt;/name
    &lt;URI&gt;http://192.168.100.213:8090/S0&lt;/URI&gt;
    &lt;content&gt;1&lt;/content&gt;
&lt;/abc:student&gt;
</code></pre>

<p>Here I want only my root elements to have <strong>abc prefix</strong> (abc:student).</p>

<p>I marshal like:</p>

<pre><code>  marshaller.marshal(new JAXBElement&lt;Student&gt;(new QName("http://example.com/xml/protocols", "student", "abc"), Student.class, (Student)object), outputStream);
</code></pre>

<p>So I add <code>abc</code> prefix to my element.</p>

<p>However when I unmarshall I get exception:</p>

<pre><code>javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.example.com/xml/protocols", local:"student"). Expected elements are &lt;{}student&gt;
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:243)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:238)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:105)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1048)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:483)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:465)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:135)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:506)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:376)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:602)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3116)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:880)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:846)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:775)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:628)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:203)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:175)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)
</code></pre>

<p>I know this exception is getting thrown as it's expecting an element without abc prefix (<code>abc:student</code>).</p>

<p>So if I add <strong>namespace</strong> inside <code>@XmlRootElement</code> like:</p>

<pre><code>    @XmlRootElement(name = "student", namespace="http://example.com/xml/protocols")
    public class Student
        extends Regular{

}
</code></pre>

<p>This exception is not thrown. Now I have many such model classes all in same package. I want to add <strong>namespace</strong> in <code>@XmlRootElement</code> in all these.</p>

<p>I don't want to edit my so many classes manually.</p>

<p>Is there a way to add namespace to all <code>@XmlRootElements</code> in a package ??</p>

<p>Or am I doing something wrong ??</p>

<p><strong>How to add a prefix to only root elements ??</strong>
If I add namespace in <strong>package-info</strong>, all elements get prefix <strong>abc</strong> which I don't want.</p>

<pre><code>@XmlSchema(
        elementFormDefault=XmlNsForm.QUALIFIED,
        namespace="http://example.com/xml/protocols"  
        xmlns={@XmlNs(prefix="abc", 
                      namespaceURI="http://example.com/xml/protocols")}
    )
</code></pre>