kuma
kuma

Reputation: 41

JAXB marshalling/unmarshalling without any configuration

I'm trying to marshal/unmarshal Java ValueObject class through JAXB.

To do it, I found that it requires XML Schema file and ObjectFactory class that can be automatically created by ant.

If so, it seems to be able to get marshaled/unmarshaled WITHOUT XML Schema file and ObjectFactory, because they can be automatically created.

But as long as I researched, somehow JAXB doesn't provide the way.

Do you know any way to do it?

Upvotes: 2

Views: 4424

Answers (3)

Leo
Leo

Reputation: 75

i'm not an expert in JAXB but you can create the unmarshaller whith this constructor:

EDIT- sorry wrong code xD

JAXBContext jaxbContext = JAXBContext.newInstance(Class1.class, Class2.class, ...);
Marshaller marshaller = jaxbContext.createMarshaller();

In this question

Use JAXB unmarshalling in Weblogic Server

You can see that i use a weblogic JAX-RPC autogenerated classes in the unmarshall of a XML. There are not ObjectFactory and this work for me... without attributes. I suposse that JAXB uses reflection to do this. Try to unmarshall with this constructor. If its not a solution use the annotations (follow Blaise link) Think in composition if you can't modify ValueObject class.

Upvotes: 0

Victor
Victor

Reputation: 17077

Your understanding is partially correct in my opinion. If you are starting from the Java ValueObject class, then there is no need for schema...you can refer to the example pointed by Blaise. But sometimes, the ValueObject class is not given to you. Instead an xml schema definition is given. So using ANT as you say....you can generate the ValueObject class from the xml schema.

So to iterate, in your case, since you have ValueObject already, no need for schema

Upvotes: 0

bdoughan
bdoughan

Reputation: 148977

JAXB does not require a schema, it is designed to start from Java objects. You then add annotations to customize the mapping to XML. Below are some useful examples to get started:

Check out my blog for more JAXB examples that start with Java objects:

Upvotes: 2

Related Questions