users@jaxb.java.net

Re: Bind existing class to existing schema HOWTO

From: Sekhar Vajjhala <Sekhar.Vajjhala_at_Sun.COM>
Date: Mon, 20 Jun 2005 23:11:38 -0400

Dmitri Colebatch wrote:

>Hi all,
>
>I figured I'd start by making a few notes on how what I'm learning so
>that (a) others can correct me where I'm missing something, and (b)
>someone else can benefit from it. What follows here is a very basic
>outline of a few steps that I'm finding myself going through when
>serializing an existing class.
>
It is useful to capture the steps. My comments are inlined below.

>
>Step one - decide whether you want to serialize fields (ie field
>declarations) or properties (get/set methods). By default properties
>are serialized. You need to put either:
>
> @XmlAccessorType(AccessType.FIELD)
>
>or
>
> @XmlAccessorType(AccessType.PROPERTY)
>
>at the top of your class depending on which approach you want to take.
>
Two comments:
a. There will be additional options as I indicated in my previous email.
b. @XmlAccessorType does not have to be at the top of the class.
    It can be used as a package level annotation. This default will then
    apply to all fields/properties accordingly. This is less tedious than
    annotating each class.

>
>
>Step two - identify the root element for the class, add the
>appropriate annotation to the top of the class:
>
> @XmlRootElement(name = "ElementName", namespace = "ElementNamespace")
>
A general comment that applies to all your steps below.

Annotations elements are defaulted as far as possible to make them
easier to use.
This is important for Java -> schema since it reduces the amount of code
that needs to be annotated by a devloper. So for e.g.

@XmlRootElement Foo {...}

will by default associate a gobal element "foo" in the target namespace
associated
with the package containing the class foo.

>
>Having done this, give it a quick test, if you have something vaguely
>resembling a javabean JAXB should spit something out - the top level
>element will be ok, but you may find you need to tweak various bits
>and pieces.
>
For java->schema, JAXB 2.0 does really try to handle as many of the
existing classes as possible.
So non Javabeans can be handled using @XmlJavaTypeAdapter.
By tweak are you talking about tweaking bits and pieces, are you
referring to
tweaking application code or adding annotations to get the desired
XML/schema ?

>
>I've found setting field access (step one) is easiest (so far - I've
>only just started!). So the rest of this assumes you're using field
>access.
>
>Step three - identify the type of the element, and the order of the
>fields to be serialized. I'm assuming that your element is a complex
>type, and it contains a sequence or all group. To identify the type
>of element use the XmlType annotation. If your element contains an
>anonymous type do this:
>
> @XmlType(name = "")
>
Yes.

>
>or if you have a global type, do this:
>
> @XmlType(name = "TypeName, namespace = "TypeNamespace")
>
Only if defaults are not suitable..

>
>Step four - Specify the order of the fields to be serialized. We do
>this by adding the propOrder attribute (???) to the XmlType
>annotation.
>
>Suppose you have two fields, foo and bar, and you want
>foo to appear before bar, do this (note that I'm assuming you're using
>an anonymous type, if you're using a global type change this to suit):
>
> @XmlType(name = "", propOrder = { "foo", "bar" })
>
>Step five - Customize the mappings. Suppose you want foo to map to an
>element called "SomethingElse" then find the field foo:
>
> private String foo;
>
>and annotate it as follows:
>
> @XmlElement(name = "SomethingElse", namespace =
>"ElementNamespace", type=String.class)
> private String foo;
>
The type() annotation parameter is redundant. It can be inferred from the
type of the field.

>
>Note also the namespace and type of the field.
>
>Step six - Customize other types. Suppose bar is in fact another type:
>
> private MyClass bar;
>
>public class MyClass
>{
> private String a;
> private String b;
>
> // .... use your imagination!
>}
>
>and you want to map Bar to an element called "SomeRandomName", then
>you need to do two things. Firstly, lets tell the field decl in our
>first class that's the name of the element:
>
> @XmlElement(name = "SomeRandomName" namespace = "Namespace")
> private MyClass bar;
>
>Secondly, we need to tell JAXB how to serialize MyClass.
>
Only if deafults are not suitable.

>This is much the same as for the top level class so I wont go through it again.
>
>And that concludes my learning! Hopefully this is (a) accurate, (b)
>sensible, and (c) helpful. I'll keep adding to this I think, and will
>look forward to any constructive criticism or questions.
>
>cheers
>dim
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>
>
Regards,
Sekhar Vajjhala