File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Java objects to XML, possibly using JAXB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java objects to XML, possibly using JAXB" Watch "Java objects to XML, possibly using JAXB" New topic

Java objects to XML, possibly using JAXB

Kirsty Beaton

Joined: Dec 06, 2010
Posts: 9
Hello all,

I am creating a simple Web Service. For the time being, I just want to have 3 or 4 Java object instances that will persist, that I can perform queries on. I want to have these instances stored in an XML file which I can read and write to.
Right now my problem is writing the objects to XML.
It seems like the best way to go about doing this is to create a class for each object in Java, then in a driver of some sort create instances of these objects, and write them to XML using JAXB.

I've read some tutorials on JAXB and with the help of Netbeans' generated code for marshalling, I have managed to write one instance to XML. My question is, what is the best way to write a group of instances into one XML file? Do I need to write a class for an object that represents a collection of the objects (for example, I want to store a group of "Book" objects, do I need to write a "BookList" class, and write one instance of "BookList" using JAXB)?

I have also read that for JAXB to work I need to have an XML schema, an "xsd" file. Is this something I write myself?

I am fairly new to XML so any help would be appreciated.
Thank you.
Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

If you just want a simple way to turn Java objects into XML, there are existing options, such as java.beans.XMLEncoder and XMLDecoder, which are part of the core API, or a 3rd-party library such as XStream (which I think is free).
I agree. Here's the link:
subject: Java objects to XML, possibly using JAXB
It's not a secret anymore!