This week's book giveaway is in the Spring forum.
We're giving away four copies of Java Persistence with Spring Data and Hibernate and have Cătălin Tudose on-line!
See this thread for details.
Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Java objects to XML, possibly using JAXB

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic