users@jaxb.java.net

Dealing with types only known at runtime

From: tdtappe <tappe_at_transdata.net>
Date: Thu, 27 Oct 2011 07:51:15 -0700 (PDT)

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.