• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best/Easiest way to write an object to an XML file  RSS feed

 
Dwayne Barsotta
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking for suggestions;

I have started a simple program that will contain several objects "Questions".  Each "Questions" object contains methods variables including an ArrayList<String> of wrong answers and a String value of the correct answer.

I have developed the "Questions" class as well as a working "test" class creates a few questions then randomly prints the questions and possible answers to the console, including the correct answer.  I have not yet coded the function for the user to choose the correct answer yet.  I'll do this when I start designing the GUI side of this application.

I am looking for alternatives for writing all the Questions to a file, like an XML file.  Then when the program loads in the future it reads the file and creates the questions.  I am looking at saving all the questions into one XML file or maybe another idea if someone has a better idea.

Here is the code for the "Questions.java" class:




The testing code is like this:




The test code randomizes the question each time its run and also randomizes the answers so that are not in the same position each time the question is asked.  Now that I have a working "Questions" class and can effectively test it.  I want to write the ability to save all the "Questions" objects to a single file (I was thinking an XML file) and then be able to read said file, building new "Question" objects as I go.  My idea is that in the future I can build an application that allows me to write new questions and answers, save them to a file, use that file for a user to update the questions in the original testing app.

My question is in everyone's opinion what would be the best approach to save the "questions" objects?  If XML, what is the favorite API as I have found so many options out there.

I really don't care about the user reading the questions in plan text as this is a self-study test guide, so if you cheat then that's your problem.  I plan to add a menu option to "upgrade questions", when selected the user will be asked for an XML file.  The file given will then be saved to the application directory. 

My main issue right now is finding a way to save and read the questions from a file.  In the end, we could be talking of 500 or so questions or more.

Any suggestions are appreciated!   

   

 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd suggest looking into the XMLEncoder class.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also asked about alternatives. CSV, JSON and XML are the most common formats to store data from an object.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I'd suggest looking into the XMLEncoder class.

I'd prefer to have some more control, and use JAXB instead.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!