0

I want to have something below to save in the xml file

Question0 : What is ur name?
 Answer0: Tina
   Question0.0 : What are your hobbies?
           Answer0.0.0 : reading 
               Question0.0.0.0 :What do you like in reading.?
                      Answer0.. : .....
           Answer0.0.1 : Dancing
               Question0.0.0.1 :which dance do you like?

 Answer1:Roger 
   ...........same question answer as above.

There can be nested question below an answer but below a question there can only be answers.

What will be the proper way to write into xml suing java. I can't think of a way....? Step by step solution to me will be great as I am new to java.

1 Answer 1

2

The easiest way is probably to use JAXB.

It consists in writing JavaBeans holding the information (in your case, a Question and an Answer class, with the Question class holding a list of answers, and the answer class holding a (sub-)question).

Then, you ave to annotate the classes to tell JAXB how to map the JavaBean properties to XML attributes or elements.

And then you have to get a Marshaller which will transform your JavaBean tree into an XML document.

The following page has a short tutorial over these steps. Googling for JAXB will lead you to a more exhaustive documentation: http://www.vogella.de/articles/JAXB/article.html#jaxb

Sign up to request clarification or add additional context in comments.

5 Comments

Okay tell me first how can i create this question and answers classes.
Or you go the other way around and create a schema (xsd) that your xml has to conform to. Once you have that you can use tools to create the JAXB classes for you.
This is very basic Java. Follow the linked tutorial: The BookStore has a name (just as your Question and Answer have a text); it has a list of books (just as your question has a list of answers). Side note: don't give me orders. You're not my boss, and I'm not your slave.
well I came to know that probably i will not use jAXB so any other options avaiable...
There are dozens of API to manipulate XML in Java (SAX, DOM, dom4j, JDom, XStream, JAXB, XMLBeans, StAX, commons digester, etc.). Pick one, learn it, and then ask a question if you have a problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.