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.
Difference between revisions of "EclipseLink/UserGuide/MOXy/Type Level/Setting Up Namespace Information"
Line 178: | Line 178: | ||
<div style="width:825px"> | <div style="width:825px"> | ||
<source lang="xml"> | <source lang="xml"> | ||
− | < | + | <?xml version="1.0" encoding="UTF-8"?> |
− | + | <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> |
</source> | </source> | ||
</div> | </div> |
Revision as of 15:30, 25 July 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 can be defined in EclipseLink XML Bindings as follows:
<?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, 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 can be defined in EclipseLink XML Bindings as follows:
<?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:
<?xml version="1.0" encoding="UTF-8"?> <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.
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 can be defined in EclipseLink XML Bindings as follows:
<?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:
<?xml version="1.0" encoding="UTF-8"?> <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.