Skip to main content

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.

Jump to: navigation, search

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

m (Replacing page with 'Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/type_level002.htm')
 
(6 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{EclipseLink_UserGuide
+
Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/type_level002.htm
|eclipselink=y
+
|eclipselinktype=MOXy
+
|info=y
+
|api=y
+
|apis= * [http://www.eclipse.org/eclipselink/api/latest/javax/xml/bind/annotation/XmlSchema.html XmlSchema]
+
|toc=y
+
}}
+
 
+
= 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'''.
+
 
+
<div style="width:800px">
+
<source lang="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;
+
</source>
+
</div>
+
 
+
This can be defined in EclipseLink XML Bindings as follows:
+
 
+
<div style="width:800px">
+
<source lang="xml">
+
<?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>
+
</source>
+
</div>
+
 
+
Using a simple '''Customer''' class, this will produce the following XML:
+
 
+
<div style="width:800px">
+
<source lang="xml">
+
<customer xmlns="http://www.example.org/package">
+
  <name>Jane Doe</name>
+
  <account>36328721</account>
+
</customer>
+
</source>
+
</div>
+
 
+
All elements are qualified with the "<tt><nowiki>http://www.example.org/package</nowiki></tt>" namespace.
+
 
+
 
+
== Type Level ==
+
 
+
Type level annotations will override the package level namespace.
+
 
+
<div style="width:800px">
+
<source lang="java">
+
package example;
+
 
+
@XmlRootElement
+
@XmlType(namespace="http://www.example.org/type")
+
public class Customer {
+
  private String name;
+
 
+
  private String account;
+
 
+
  ...
+
}
+
</source>
+
</div>
+
 
+
This can be defined in EclipseLink XML Bindings as follows:
+
 
+
<div style="width:800px">
+
<source lang="xml">
+
<?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>
+
</source>
+
</div>
+
 
+
This will produce the following XML:
+
 
+
<div style="width:800px">
+
<source lang="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>
+
</source>
+
</div>
+
 
+
Only elements inside the '''Customer''' type are qualified with the "<tt><nowiki>http://www.example.org/type</nowiki></tt>" 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.
+
 
+
<div style="width:800px">
+
<source lang="java">
+
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;
+
 
+
  ...
+
}
+
</source>
+
</div>
+
 
+
This will produce the following XML:
+
 
+
<div style="width:800px">
+
<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">
+
    <name>Jane Doe</name>
+
    <ns2:account>36328721</ns2:account>
+
</ns3:customer>
+
</source>
+
</div>
+
 
+
Only the '''account''' element is qualified with the "<tt><nowiki>http://www.example.org/property</nowiki></tt>" namespace.
+
 
+
 
+
 
+
{{EclipseLink_MOXy
+
|previous=[[EclipseLink/UserGuide/MOXy/Type Level/Mapping to a Type or Element/Default Root Element|Default Root Element]]
+
|next=    [[EclipseLink/UserGuide/MOXy/Type Level/Handling Inheritance|Handling Inheritance]]
+
}}
+

Latest revision as of 09:49, 8 November 2012

Please see http://www.eclipse.org/eclipselink/documentation/2.4/moxy/type_level002.htm

Back to the top