users@jaxb.java.net

Re: JAXB newbie questions

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Tue, 27 Nov 2007 08:22:43 +0100

Hi.

> I am relatively new to JAXB/XSD but am having some problems, I am hoping
> someone can help with.
>
> What I am trying to do is auto-generate an 'easy' to use SDK API for an XML
> document format we wish to standardize on. I am using XSD to specify the
> XML content & structure and of course, JAXB to generate the Java classes.
>
> Here are some of the problems:
>
> 1. JAXB doesn't generate any interfaces only concrete objects. However the
> docs seem to say that everything should have an interface (and this is what
> I want).

You may use a customization to do this:

https://jaxb.dev.java.net/guide/Migrating_JAXB_1_0_applications_to_JAXB_2_x.html#Other_Miscellaneous_Notes

Although I like beans better.

> 2. The concrete objects JAXB generates aren't very smart, they are JavaBeans
> only. I would expect a factory or at least constructors that can take all
> the required parameters plus optional.

These are data objects. Should not be too smart.
You can use plugins to add features. Many of the plugins are available
here: https://jaxb2-commons.dev.java.net
For the task you describe, take
https://jaxb2-commons.dev.java.net/value-constructor/

> 3. If I have reusable elements in the XML, I would like to reference these
> without the Java API user having to set ID/IDREF. I don't see a way to do
> this. I want all XML constructs such as these shielded from the Java user.
> How can I do this? Using JAXB, is it possible to have java objects that
> reference other java objects, serialize these to XML, unserialize back to
> java objects, and have all the references intact?

Yes. See spec for ID/IDREF handling and the following section from the guide:
https://jaxb.dev.java.net/guide/Mapping_cyclic_references_to_XML.html

You may also provide a custom com.sun.xml.bind.IDResolver to handle ID
resolution in more complex cases (like, get an entity from the DB by
the given id).

Bye.
/lexi