This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am reading JAXB, and learned the steps of
Unmarshaling (bind, Create JAXBContext, getUnmarshller, call unmarshal)
I also came to know about reading XML data using
ObjectFactory (bind, Create ObjectFactory, get Collection, ...)
I am not able to understand the difference between Unmarshalling & reading XML data using ObjectFactory,
If I have a choice to use either of way to read XML file using JAXB, why should I prefer one to other?
I understand the part about unmarshalling, but not the second part about reading XML data. Can you give a code example?
If it is an ObjectFactory that you get after binding and you let it create some object for you, then it will not, in itself, help you to read or write XML.
For that, you need an Unmarshaler or a Marshaler. At least these are the techniques that I have seen so far.
An Alternative: Accessing Data without Unmarshalling: JAXB also allows you to access XML data without having to unmarshal it. One of the classes generated from a schema, ObjectFactory, contains methods to generate objects for each of the schema-derived interfaces and classes. For example, the package generated for the books.xsd schema includes an ObjectFactory class that has methods such as createCollection to create a Collection object, and createBookType to create a BookType object. You can use these methods to create a tree of content objects without doing any unmarshalling. All your program needs is access to the ObjectFactory class that's in the package for the pertinent schema. Then you can use the appropriate methods in the ObjectFactory class to create the objects you need. After you create the objects, you need to provide their content. To do that, you use the set methods in the objects.
As per above para which is from source URL, for reading using JAXB, Unmarshlling is the way but user has option of reading the XML source without Unmarshaling.
Joined: Oct 04, 2006
No, there is no XML involved in the process described in the quote. I feel that the formulation is unfortunate.
What it means is that you can create a tree of objects that correspond to some XML data by letting an ObjectFactory create the instances and then insert the data yourself, using the setter methods in the objects.
Joined: Feb 24, 2009
JAXB also allows you to access XML data without having to unmarshal it.
But you are suggesting that it's not reading the XML data but creating ContentObject based on the XML data & using setter method user is adding data to content.
It's contradicting to me. But since I have good experience with your guidance/suggestions, I would prefer to come back to this topic after spending more time on reading about JWS.