users@jaxb.java.net

Re: Dealing with types only known at runtime

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Thu, 27 Oct 2011 21:38:20 +0200

I haven't checked your examples, but it could be that Section "Elements with
Any Type"<http://jaxb.java.net/tutorial/section_2_2_16_1-Elements-With-Any-Type.html#Elements%20With%20Any%20Type>of
my JAXB tutorial might help you.
-W


On 27 October 2011 16:51, tdtappe <tappe_at_transdata.net> wrote:

>
> I am pretty new to JAXB, so this might be a stupid question. But I couldn't
> find any useful information so far. Probably because I don't know what to
> look for exactly.
>
> As long as I use static types in my objects to marshal/unmarshal
> everything's fine. Here and there I use or have to use an
> XmlJavaTypeAdapter
> - no problem.
>
> But now I need to handle objects/classes that have a field of type
> 'Object'.
> The type of the field is only known at runtime.
> Any help (hints, sample code, ...) is greatly appreciated.
>
> --Heiko
>
> Example:
>
> @XmlRootElement
> public class Test {
> public Object field;
> }
>
> public class Other {
> public String name;
> }
>
> public class YetAnother {
> public Long value;
> }
>
> jaxbContext = JAXBContext.newInstance(
> Test.class,
> Other.class
> // ...
> );
>
> Marshaller marshaller = jaxbContext.createMarshaller();
>
> Test test = new Test();
> Other other = new Other();
> other.name = "Hello world";
> test.field = other;
>
> Document document = marshaller.marshal(test);
> //...
> Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
> unmarshal(document);
>
> //...
>
> YetAnother another = new YetAnother();
> another.value = 1L;
>
> document = marshaller.marshal(test);
> unmarshal(document);
> --
> View this message in context:
> http://old.nabble.com/Dealing-with-types-only-known-at-runtime-tp32731961p32731961.html
> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
>
>