Most XML documents are qualified with a namespace. You can namespace-qualify elements of your Java class at the following levels:
In most cases, package-level annotation is sufficient. You can use the other levels to customize your document. Use the @XmlSchema
annotation to specify the namespace.
Use the @XmlSchema
annotation on the package to set a default namespace and specify that all elements in the package are qualified with the namespace. This information is specified in a special Java source file, package-info.java
.
Example 3-6 Using Annotations
@XmlSchema( namespace="http://www.example.org/package", elementFormDefault=XmlNsForm.QUALIFIED) package example; import javax.xml.bind.annotation.XmlNsForm; import javax.xml.bind.annotation.XmlSchema;
This can be defined in EclipseLink XML Bindings as follows:
Example 3-7 Using OXM Metadata
<?xml version="1.0" encoding="UTF-8"?> <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"> <xml-schema element-form-default="QUALIFIED" namespace="http://www.example.org/package"> </xml-schema> <java-types> <java-type name="Customer"> ... </xml-bindings>
Using a simple Customer class, Example 3-6 and Example 3-7 will produce the following XML:
<customer xmlns="http://www.example.org/package"> <name>Jane Doe</name> <account>36328721</account> </customer>
All elements are qualified with the http://www.example.org/package namespace.
Type level annotations will override the package level namespace.
Example 3-8 Using Annotations
package example; @XmlRootElement @XmlType(namespace="http://www.example.org/type") public class Customer { private String name; private String account; ... }
This can be defined in EclipseLink XML Bindings as follows:
Example 3-9 Using XML Bindings File
<?xml version="1.0" encoding="UTF-8"?> <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"> <xml-schema element-form-default="QUALIFIED" namespace="http://www.example.org/package"> </xml-schema> <java-types> <java-type name="Customer"> <xml-type namespace="http://www.example.org/type" /> <java-attributes> <xml-element java-attribute="name" /> <xml-element java-attribute="account" /> </java-attributes> </java-type> </java-types> </xml-bindings>
This will produce the following XML:
<custom xmlns="http://www.example.org/package" xmlns:ns0="http://www.example.org/type"> <ns0:name>Bob</ns0:name> <ns0:account>1928712</ns0:account> </custom>
Elements inside the Customer type are qualified with the http://www.example.org/type namespace.
You can override the package or type namespaces at the property/field level. All attribute and element annotations accept the namespace parameter.
Example 3-10 Overriding the Namespace
package example; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) @XmlType(namespace="http://www.example.org/type") public class Customer { private String name; @XmlElement(namespace="http://www.example.org/property") private String account; ... }
This can be defined in EclipseLink XML Bindings as follows:
Example 3-11 Sample Bindings File
<?xml version="1.0" encoding="UTF-8"?> <xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"> <xml-schema element-form-default="QUALIFIED" namespace="http://www.example.org/package"> </xml-schema> <java-types> <java-type name="Customer"> <xml-type namespace="http://www.example.org/type" /> <java-attributes> <xml-element java-attribute="name" /> <xml-element java-attribute="account" namespace="http://www.example.org/property" /> </java-attributes> </java-type> </java-types> </xml-bindings>
This will produce the following XML:
<custom xmlns="http://www.example.org/package" xmlns:ns1="http://www.example.org/property" xmlns:ns0="http://www.example.org/type"> <ns0:name>Bob</ns0:name> <ns1:account>1928712</ns1:account> </custom>
Only the account element is qualified with the http://www.example.org/property namespace.