Skip to main content

Notice: This Wiki is now read only and edits are no longer 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
m
Line 1: Line 1:
'''Example:'''
+
=Setting Up Namespace Information=
*http://bdoughan.blogspot.com/2010/08/jaxb-namespaces.html
+
Most XL documents are qualified with a namespace. You can qualify elements at the following levels:
 +
*Package
 +
*Type
 +
*Field or Property
 +
In most cases, package level annotation is sufficient. You can use the other levels to customize your document.
 +
 
 +
==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.
 +
 
 +
<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>
 +
 
 +
This will produce the following XML:
 +
 
 +
<source lang="xml">
 +
<customer
 +
    xmlns="http://www.example.org/package"
 +
    id="123">
 +
    <name>Jane Doe</name>
 +
</customer>
 +
</source>
 +
 
 +
All elements are qualified with the '''http://www.example.org/package''' namespace.
 +
 
 +
 
 +
==Type Level==
 +
Type level annotations will override the package level namespace.
 +
 
 +
<source lang="java">
 +
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;
 +
    }
 +
 +
}
 +
</source>
 +
 
 +
This will produce the following XML:
 +
 
 +
<source lang="xml">
 +
<ns2:customer
 +
    xmlns="http://www.example.org/type"
 +
    xmlns:ns2="http://www.example.org/package"
 +
    id="123">
 +
    <name>Jane Doe</name>
 +
</ns2:customer>
 +
</xml>
 +
 
 +
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.
 +
 
 +
<source lang="java">
 +
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;
 +
    }
 +
 +
}
 +
</source>
 +
 
 +
This will produce the following XML:
 +
 
 +
<source lang="xml">
 +
<ns2:customer
 +
    xmlns="http://www.example.org/property"
 +
    xmlns:ns2="http://www.example.org/package"
 +
    id="123">
 +
    <name>Jane Doe</name>
 +
</ns2:customer>
 +
</source>
 +
 
 +
The '''Name''' element is qualified with the '''http://www.example.org/property''' namespace.
 +
 
 +
 
  
  

Revision as of 14:01, 6 December 2010

Setting Up Namespace Information

Most XL documents are qualified with a namespace. You can qualify elements at the following levels:

  • Package
  • Type
  • Field or Property

In most cases, package level annotation is sufficient. You can use the other levels to customize your document.

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.

@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"
    id="123">
    <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>
</xml>
 
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.
 
<source lang="java">
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.




Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source


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

Back to the top