I'm trying to output the schemaLocation
attribute properly when marshalling an xjc
-generated class instance. The root element class looks like:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"rootElement"
})
@XmlRootElement(name = "ROOTELEMENT")
public class ROOTELEMENT {
// Members
}
I see there's a package-info.java
class sitting in the package where all generated classes are, with the following contents:
@javax.xml.bind.annotation.XmlSchema(
namespace = "http://my.own.namespace",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package my.generated.classes.package;
The answer to JAXB XJC code generation - “schemaLocation” missing in xml generated by Marshaller proposes setting the Marshaller.JAXB_SCHEMA_LOCATION
property, and it indeed works:
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://my.own.namespace my_schema.xsd");
But I'd like to avoid typing the namespace, as in:
String namespace = getNamespace(rootElementInstance);
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, namespace + " my_schema.xsd");
I'd appreciate any tips on how to implement the getNamespace()
function.
EDIT: I've seen that the XmlRootElement
and XmlType
annotations have the namespace()
method, that seems to be what I'm after (actually, they seem to delegate on the XmlSchema
provided in package-info.java
). However, I cannot get an instance of ROOTELEMENT
casted to any of these types.