wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
Author

Java objects to XML, possibly using JAXB

Kirsty Beaton
Greenhorn

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
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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: http://aspose.com/file-tools
 
subject: Java objects to XML, possibly using JAXB
 
Similar Threads
Difference between (Java to XML) binding and serialization, JAXB and XStream
Generalized Parsing for SOAP Response
Facade design pattern in web services
Data class Singleton question
JAX-WS Question