JAXB: How to marshal without repeated namespace
<p>My current result of marshalling is this document:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
&lt;rootElem version="1"&gt;
  &lt;list&gt;
    &lt;entry&gt;
      &lt;key xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string"&gt;someValue&lt;/key&gt;
      &lt;value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string"&gt;42&lt;/value&gt;
    &lt;/entry&gt;
    &lt;entry&gt;
      &lt;key xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string"&gt;otherValue&lt;/key&gt;
      &lt;value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string"&gt;24&lt;/value&gt;
    &lt;/entry&gt;
  &lt;/list&gt;
&lt;/rootElem&gt;
</code></pre>

<p>and I want for marshaller to extract the NS so it doesn't repeat on each element, like this:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
&lt;rootElem version="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
  &lt;list&gt;
    &lt;entry&gt;
      &lt;key xsi:type="xs:string"&gt;someValue&lt;/key&gt;
      &lt;value xsi:type="xs:string"&gt;42&lt;/value&gt;
    &lt;/entry&gt;
    &lt;entry&gt;
      &lt;key xsi:type="xs:string"&gt;otherValue&lt;/key&gt;
      &lt;value xsi:type="xs:string"&gt;24&lt;/value&gt;
    &lt;/entry&gt;
  &lt;/list&gt;
&lt;/rootElem&gt;
</code></pre>

<p>How can this be done?</p>

<p>I've found <a href="https://stackoverflow.com/a/2354596/451962">an answer</a> to this problem, but it doesn't solve the problem completely (it just moves the instance to the rootElem).</p>

<p>This is how I marshall it:</p>

<pre><code>JAXBContext jc = JAXBContext.newInstance(RootElem.class);
Marshaller m = jc.createMarshaller();
m.setProperty("jaxb.formatted.output", true);
m.marshal(rootElem, file);
</code></pre>