JAXB: Namespace missing
java, jaxb, namespaces, xml
Solution
YOUR MAPPINGS ARE CORRECT
I tried out your model and it works for me:
Demo
package ws.avail;
import java.io.StringWriter;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
OTAHotelAvailNotifRQ rootElement = new OTAHotelAvailNotifRQ();
StringWriter stringWriter = new StringWriter();
JAXBContext context = JAXBContext.newInstance(rootElement.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(rootElement, stringWriter);
System.out.println(stringWriter.toString());
}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><OTA_HotelAvailNotifRQ xmlns="http://www.opentravel.org/OTA/2003/05"/>
SOLUTION
Usually when this problem occurs the `package-info` class was not compiled or not packaged with the application.
FOR MORE INFORMATION
- http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
Problem
I am trying to serialize an XML from a class generated via JAXB. The class: ``` package ws.avail; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "pos", "uniqueID", "availStatusMessages" }) @XmlRootElement(name = "OTA_HotelAvailNotifRQ") public class OTAHotelAvailNotifRQ { ... } ``` File "package-info.java": ``` @javax.xml.bind.annotation.XmlSchema(namespace = "http://www.opentravel.org/OTA/2003/05", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) package ws.avail; ``` The marshaller (simplified without error handling): ``` context = JAXBContext.newInstance(rootElement.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.marshal(rootElement, stringWriter); return stringWriter.toString(); ``` This code generates: ``` <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <OTA_HotelAvailNotifRQ Version="1.000" Target="TEST"> .... ``` I expect something like this: ``` <?xml version="1.0" encoding="UTF-8"?> <OTA_HotelAvailNotifRQ xmlns="http://www.opentravel.org/OTA/2003/05" Version="1.000" Target="TEST"> .... ``` I have a similar code and works correctly, but I am unable to see why this code doesn't display the namespace. Any clue? Thanks!