Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
647 views
in Technique[技术] by (71.8m points)

java - structuring things in XML file

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...