I'm new to using namespaces in xml so I am kind of confused and would like some clarification. I have a java service where I am receiving xml documents with many different namespaces and while i got it working, I feel like I must have done something wrong so I want to check. In my package-info.java I have my schema annotation such as:
@javax.xml.bind.annotation.XmlSchema(
xmlns={
@javax.xml.bind.annotation.XmHS(prefix="train", namespaceURI="http://mycompany/train"),
@javax.xml.bind.annotation.XmHS(prefix="passenger", namespaceURI="http://mycompany/passenger")
},
elementFormDefault = javax.xml.bind.annotation.XmlNsForm=QUALIFIED
)
I have a Train.java annotated on the class level with:
@XmlRootElement(name="Train", namespace="http://mycompany/train")
and each field in the class annotated with:
@XmlElement(name="Color") for example
Train contains a List of Passenger(s) so there's a property
private Set<Passenger> passengers;
and this collection is annotated with:
@XmlElementWrapper(name="Passengers")
@XmlElements(@XmlElement(name="Passenger", namespace="http://mycompany/passenger"))
Then within Passenger.java the class itself is annotated with:
@XmlElement(name="Passenger", namespace="http://mycompany/passenger")
Finally for individual fields within Passenger.java, they are annotated like this:
@XmlElement(name="TicketNumber", namespace="http://mycompany/passenger")
So when I have an xml that looks like:
<train:Train>
<train:Color>Red</train:Color>
<train:Passengers>
<train:Passenger>
<passenger:TicketNumber>T101</passenger:TicketNumber>
</train:Passenger>
</train:Passengers>
</train:Train>
Now I unmarshal this xml I received and Train's Color property is set and Passenger's TicketNumber property is set. But I don't know why I need to add the namespace url on the XmlElement annotation on TicketNumber for that to work but I didn't need to do so for the Color property on Train. If I remove the namespace attribute from the XmlElement annotation on TicketNumber, the value from the xml wont get mapped to the object unless I also remove the namespace prefix from the xml request. I feel like since I've got the namespace attribute defined on the XmlRootElement for Passenger, I shouldn't need to do that for every single field in the class as well just like I didn't have to for Train so I am assuming I must have setup something wrong. Can someone point me in the right direction? Thanks!
Below is an explanation of how namespaces work in JAXB (JSR-222) based on your model.
JAVA MODEL
package-info
Below is a modified version of your
@XmlSchema
annotation. It contains some key information:namespace
- The default namespace that will be used to qualify global elements (those corresponding to@XmlRootElement
and@XmlElementDecl
annotations (and local elements based on theelementFormDefault
value) that don't have another namespace specified.elementFormDefault
by default only global elements are namespace qualified but by setting the value to beXmlNsForm.QUALIFIED
all elements without an explicit namespace specified will be qualified with thenamespace
value.xmlns
is the preferred set of prefixes that a JAXB impl should use for those namespaces (although they may use other prefixes).Train
Since all the elements corresponding to the
Train
class correspond to thenamespace
specified on the@XmlSchema
annotation, we don't need to specify any namespace info.@XmlRootElement
annotation corresponds to a global element.@XmlElementWrapper
and@XmlElement
annotations correspond to local elements.Passenger
If all the elements corresponding to properties on the
Passenger
class will be in thehttp://mycompany/passenger
namespace, then you can use the@XmlType
annotation to override thenamespace
from the@XmlSchema
annotation.Alternatively you can override the namespace at the property level.
DEMO CODE
The following demo code can be run to prove that everything works:
Demo
input.xml/Output
In the XML below I have added the necessary namespace declarations that were missing from the XML document in your question.
FOR MORE INFORMATION