1

Matching namespaces on marshalling / unmarshalling in JAXB

I use JAXB to marshall / unmarshall XML. If I marshall an XML file like this:

<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">
    <om:event>
        <v1:applicationDN>System/JSR264/ApplicationType/OrderManagement/Application/1-0;1-0-2;ReferenceImplementation/</v1:applicationDN>
        <v1:eventTime>2008-11-12T07:39:34.896+01:00</v1:eventTime>
        <om:requestValue xmlns:v1="http://ossj.org/xml/om/ri/omimpl/v1-0" xsi:type="v1:ProductOrderImplValue">
            <v13:key xmlns:v1="http://ossj.org/xml/OrderManagement/v1-0" xmlns:v13="http://ossj.org/xml/Common/v1-5" xsi:type="v1:ProductOrderKey">
                <v13:type>http://ossj.org/xml/om/ri/omimpl/v1-0#ProductOrderImplValue</v13:type>
                <v13:primaryKey>12</v13:primaryKey>
            </v13:key>
            <v1:requestState>open.not_running.not_started</v1:requestState>
            <v12:description xsi:nil="true"/>
        </om:requestValue>
    </om:event>
</om:RequestCreateEvent>

and then subsequently try to unmarshall it, I get this:

<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">
    <ns4:event>
        <applicationDN>System/JSR264/ApplicationType/OrderManagement/Application/1-0;1-0-2;ReferenceImplementation/</applicationDN>
        <eventTime>
2008-11-12T07:39:34.896+01:00</eventTime>
        <ns4:requestValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns5:ProductOrderImplValue">
            <key xsi:type="ns4:ProductOrderKey">
                <type>
http://ossj.org/xml/om/ri/omimpl/v1-0#ProductOrderImplValue</type>
                <primaryKey/>
            </key>
            <ns5:requestState>open.not_running.not_started</ns5:requestState>
            <ns3:description xsi:nil="true"/>
        </ns4:requestValue>
    </ns4:event>
</ns4:RequestCreateEvent>

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.

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.

So if anyone has any suggestions on a NamespaceContext solution or indeed some other approach I would appreciate it.

1
  • Did you also notice that <v13:primaryKey>12</v13:primaryKey> changed to <primaryKey/> when unmarshalled? Curious if you were able to overcome this issue as I'm running into this very problem
    – JGlass
    Oct 26, 2017 at 20:43

1 Answer 1

0

After a quick look at your XML documents that appear to be namespace qualified the same but use different prefixes. Different JAXB implementations provide different mechanisms for controlling the prefixes used, but will involve interaction on your part:

JAXB RI/Metro JAXB

The JAXB RI/Metro offer an extension called the NamespacePrefixMapper:

EclipseLink JAXB (MOXy)

MOXy (I'm the tech lead) will use the prefixes specified in the @XmlSchema annotation. In the example below the atom prefix will be used for the http://www.w3.org/2005/Atom namespace URI.

javax.xml.bind.annotation.XmlSchema(  
    xmlns = {  
          @javax.xml.bind.annotation.XmlNs(prefix = "atom", namespaceURI = "http://www.w3.org/2005/Atom")  
            })  
    package org.example.domain;  
1
  • thanks for getting back to me. By adding a class which extends NamespaceMapper, I can get the XML out as I intend. However I still need to hard-code the prefix and their associated namespaces in the getPreferredPrefix() method of this class. At runtime, I dont have this information. I can see that the UnmarshallingContext of the unmarshaller I'm using has them, but I cannot seem to access them. Do you know of any way of accessing namespaces / prefixes from unmarshaller?
    – belltower
    Nov 2, 2011 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.