title: Matching namespaces on marshalling / unmarshalling in JAXB
body: <p>Matching namespaces on marshalling / unmarshalling in JAXB</p>
  
  <p>I use JAXB to marshall / unmarshall XML. If I marshall an XML file like this:</p>
  
  <p><code></p>
  
  <pre><code>&lt;om:RequestCreateEvent xmlns:om="http://ossj.org/xml/OrderManagement/v1-0" xmlns:v1="http://ossj.org/xml/Common/v1-5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:v11="http://ossj.org/xml/Common-CBECore/v1-5" xmlns:v12="http://ossj.org/xml/Common-CBEBi/v1-5"&gt;
      &lt;om:event&gt;
          &lt;v1:applicationDN&gt;System/JSR264/ApplicationType/OrderManagement/Application/1-0;1-0-2;ReferenceImplementation/&lt;/v1:applicationDN&gt;
          &lt;v1:eventTime&gt;2008-11-12T07:39:34.896+01:00&lt;/v1:eventTime&gt;
          &lt;om:requestValue xmlns:v1="http://ossj.org/xml/om/ri/omimpl/v1-0" xsi:type="v1:ProductOrderImplValue"&gt;
              &lt;v13:key xmlns:v1="http://ossj.org/xml/OrderManagement/v1-0" xmlns:v13="http://ossj.org/xml/Common/v1-5" xsi:type="v1:ProductOrderKey"&gt;
                  &lt;v13:type&gt;http://ossj.org/xml/om/ri/omimpl/v1-0#ProductOrderImplValue&lt;/v13:type&gt;
                  &lt;v13:primaryKey&gt;12&lt;/v13:primaryKey&gt;
              &lt;/v13:key&gt;
              &lt;v1:requestState&gt;open.not_running.not_started&lt;/v1:requestState&gt;
              &lt;v12:description xsi:nil="true"/&gt;
          &lt;/om:requestValue&gt;
      &lt;/om:event&gt;
  &lt;/om:RequestCreateEvent&gt;
  </code></pre>
  
  <p></code></p>
  
  <p>and then subsequently try to unmarshall it, I get this:
  <code></p>
  
  <pre><code>&lt;ns4:RequestCreateEvent xmlns="http://ossj.org/xml/Common/v1-5" xmlns:ns2="http://ossj.org/xml/Common-CBECore/v1-5" xmlns:ns3="http://ossj.org/xml/Common-CBEBi/v1-5" xmlns:ns4="http://ossj.org/xml/OrderManagement/v1-0" xmlns:ns5="http://ossj.org/xml/om/ri/omimpl/v1-0" xmlns:ns6="http://ossj.org/xml/Common-CBEDatatypes/v1-5" xmlns:ns7="http://ossj.org/xml/Common-CBELocation/v1-5" xmlns:ns8="http://ossj.org/xml/Common-CBEResource/v1-5" xmlns:ns9="http://ossj.org/xml/Common-CBEService/v1-5" xmlns:ns10="http://ossj.org/xml/Common-CBEProduct/v1-5" xmlns:ns11="http://ossj.org/xml/Common-CBEProductOffering/v1-5" xmlns:ns12="http://ossj.org/xml/Common-CBEParty/v1-5"&gt;
      &lt;ns4:event&gt;
          &lt;applicationDN&gt;System/JSR264/ApplicationType/OrderManagement/Application/1-0;1-0-2;ReferenceImplementation/&lt;/applicationDN&gt;
          &lt;eventTime&gt;
  2008-11-12T07:39:34.896+01:00&lt;/eventTime&gt;
          &lt;ns4:requestValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns5:ProductOrderImplValue"&gt;
              &lt;key xsi:type="ns4:ProductOrderKey"&gt;
                  &lt;type&gt;
  http://ossj.org/xml/om/ri/omimpl/v1-0#ProductOrderImplValue&lt;/type&gt;
                  &lt;primaryKey/&gt;
              &lt;/key&gt;
              &lt;ns5:requestState&gt;open.not_running.not_started&lt;/ns5:requestState&gt;
              &lt;ns3:description xsi:nil="true"/&gt;
          &lt;/ns4:requestValue&gt;
      &lt;/ns4:event&gt;
  &lt;/ns4:RequestCreateEvent&gt;
  </code></pre>
  
  <p></code></p>
  
  <p>I need to ensure that the prefixes used in the namespaces when I marshall the jaxb generated pojo to create the XML file match those used when 
  I unmarshall the same file.</p>
  
  <p>Possibly there is a solution in using a NamespaceContext when I do the marshalling. However I cannot hard-code the prefixes and their uris in an implementation
  of NamespaceContext because I do not have this information available to me(I use lots of schemas etc). So if I were to try and use a NamespaceContext I would need to be able to
  get the prefixes and their uris from the JAXB unmarshaller which I cant seem to get.</p>
  
  <p>So if anyone has any suggestions on a NamespaceContext solution or indeed some other approach I would appreciate it.</p>