File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Services Certification (SCDJWS/OCEJWSD) and the fly likes JAX-B - Unmarshalling Vs. Read XML data (ObjectFactory) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Services Certification (SCDJWS/OCEJWSD)
Bookmark "JAX-B - Unmarshalling Vs. Read XML data (ObjectFactory)" Watch "JAX-B - Unmarshalling Vs. Read XML data (ObjectFactory)" New topic
Author

JAX-B - Unmarshalling Vs. Read XML data (ObjectFactory)

Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
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?
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
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.
Best wishes!
Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
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.
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
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.
Best wishes!
Deepika Joshi
Ranch Hand

Joined: Feb 24, 2009
Posts: 268
hmmm...
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.

Thanks.
 
 
subject: JAX-B - Unmarshalling Vs. Read XML data (ObjectFactory)
 
Similar Threads
JAXB, DOM, SAX
Facade design pattern in web services
how to convert one string to JAXBElement
extending or instantiating JAXB factory?
No Class def found error: JAXBContext