Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/UserGuide/MOXy/Type Level/Setting Up Namespace Information"
Line 14: | Line 14: | ||
* Package | * Package | ||
* Type | * Type | ||
− | * Field | + | * Field / Property |
In most cases, package-level annotation is sufficient. You can use the other levels to customize your document. Use the the <tt>@XmlSchema</tt> annotation to specify the namespace. | In most cases, package-level annotation is sufficient. You can use the other levels to customize your document. Use the the <tt>@XmlSchema</tt> annotation to specify the namespace. | ||
Line 20: | Line 20: | ||
== Package Level == | == Package Level == | ||
− | Use the | + | Use the <tt>@XmlSchema</tt> 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, <tt>package-info.java</tt>. |
<source lang="java"> | <source lang="java"> | ||
@XmlSchema( | @XmlSchema( | ||
− | + | namespace="http://www.example.org/package", | |
− | + | elementFormDefault=XmlNsForm.QUALIFIED) | |
package example; | package example; | ||
Line 35: | Line 35: | ||
<source lang="xml"> | <source lang="xml"> | ||
− | <customer | + | <customer xmlns="http://www.example.org/package"> |
− | + | <name>Jane Doe</name> | |
− | + | ||
− | + | ||
</customer> | </customer> | ||
</source> | </source> | ||
− | All elements are qualified with the | + | All elements are qualified with the "<tt>http://www.example.org/package</tt>" namespace. |
− | + | ||
==Type Level== | ==Type Level== |
Revision as of 16:56, 6 January 2011
EclipseLink MOXy
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Key API
Contents
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;
This will produce the following XML:
<customer xmlns="http://www.example.org/package"> <name>Jane Doe</name> </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; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement @XmlType(namespace="http://www.example.org/type") public class Customer { private long id; private String name; @XmlAttribute public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
This will produce the following XML:
<ns2:customer xmlns="http://www.example.org/type" xmlns:ns2="http://www.example.org/package" id="123"> <name>Jane Doe</name> </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; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement @XmlType(namespace="http://www.example.org/type") public class Customer { private long id; private String name; @XmlAttribute public long getId() { return id; } public void setId(long id) { this.id = id; } @XmlElement(namespace="http://www.example.org/property") public String getName() { return name; } public void setName(String name) { this.name = name; } }
This will produce the following XML:
<ns2:customer xmlns="http://www.example.org/property" xmlns:ns2="http://www.example.org/package" id="123"> <name>Jane Doe</name> </ns2:customer>
The Name element is qualified with the http://www.example.org/property namespace.