Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Type Level/Setting Up Namespace Information"

Line 24: Line 24:
 
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'''.
 
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'''.
  
<div style="width:700px">
+
<div style="width:800px">
 
<source lang="java">
 
<source lang="java">
 
@XmlSchema(
 
@XmlSchema(
Line 38: Line 38:
 
Using a simple '''Customer''' class, this will produce the following XML:
 
Using a simple '''Customer''' class, this will produce the following XML:
  
<div style="width:700px">
+
<div style="width:800px">
 
<source lang="xml">
 
<source lang="xml">
 
<customer xmlns="http://www.example.org/package">
 
<customer xmlns="http://www.example.org/package">
Line 54: Line 54:
 
Type level annotations will override the package level namespace.
 
Type level annotations will override the package level namespace.
  
<div style="width:700px">
+
<div style="width:800px">
 
<source lang="java">
 
<source lang="java">
 
package example;
 
package example;
Line 72: Line 72:
 
This will produce the following XML:
 
This will produce the following XML:
  
<div style="width:700px">
+
<div style="width:800px">
 
<source lang="xml">
 
<source lang="xml">
 
<ns2:customer xmlns="http://www.example.org/type" xmlns:ns2="http://www.example.org/package">
 
<ns2:customer xmlns="http://www.example.org/type" xmlns:ns2="http://www.example.org/package">
Line 88: Line 88:
 
You can override the package or type namespaces at the property/field level.  All attribute and element annotations accept the '''namespace''' parameter.
 
You can override the package or type namespaces at the property/field level.  All attribute and element annotations accept the '''namespace''' parameter.
  
<div style="width:700px">
+
<div style="width:800px">
 
<source lang="java">
 
<source lang="java">
 
package example;
 
package example;
Line 108: Line 108:
 
This will produce the following XML:
 
This will produce the following XML:
  
<div style="width:700px">
+
<div style="width:800px">
 
<source lang="xml">
 
<source lang="xml">
 
<ns3:customer xmlns="http://www.example.org/type" xmlns:ns2="http://www.example.org/property" xmlns:ns3="http://www.example.org/package">
 
<ns3:customer xmlns="http://www.example.org/type" xmlns:ns2="http://www.example.org/property" xmlns:ns3="http://www.example.org/package">

Revision as of 13:45, 25 July 2011

EclipseLink MOXy

link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source

Elug api package icon.png Key API

Setting Up Namespace Information

Most XML documents are qualified with a namespace. You can namespace-qualify elements of your Java class at the following levels:

  • Package
  • Type
  • Field / Property


In most cases, package-level annotation is sufficient. You can use the other levels to customize your document. Use the the @XmlSchema annotation to specify the namespace.


Package Level

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.

@XmlSchema(
   namespace="http://www.example.org/package",
   elementFormDefault=XmlNsForm.QUALIFIED)
package example;
 
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

Using a simple Customer class, this 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

Type level annotations will override the package level namespace.

package example;
 
@XmlRootElement
@XmlType(namespace="http://www.example.org/type")
public class Customer {
   private String name;
 
   private String account;
 
   ...
}

This will produce the following XML:

<ns2:customer xmlns="http://www.example.org/type" xmlns:ns2="http://www.example.org/package">
    <name>Jane Doe</name>
    <account>36328721</account>
</ns2:customer>

Only elements inside the Customer type are qualified with the "http://www.example.org/type" namespace.


Field/Property Level

You can override the package or type namespaces at the property/field level. All attribute and element annotations accept the namespace parameter.

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 will produce the following XML:

<ns3:customer xmlns="http://www.example.org/type" xmlns:ns2="http://www.example.org/property" xmlns:ns3="http://www.example.org/package">
    <name>Jane Doe</name>
    <ns2:account>36328721</ns2:account>
</ns3:customer>

Only the account element is qualified with the "http://www.example.org/property" namespace.


Eclipselink-logo.gif
Version: DRAFT
Other versions...