在XML文件中构造事物 [英] structuring things in XML file
问题描述
我想在xml文件中保存以下内容
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.
写入xml suing java的正确方法是什么。我想不出办法......?对我来说,一步一步的解决方案将很棒,因为我是java的新手。
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.
推荐答案
最简单的方法可能是使用JAXB。
The easiest way is probably to use JAXB.
它包括编写保存信息的JavaBeans(在您的情况下,问题
和回答
类,其中Question类包含一个答案列表,而答案类包含(子)问题)。
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).
然后,您需要注释类以告诉JAXB如何将JavaBean属性映射到XML属性或元素。
Then, you ave to annotate the classes to tell JAXB how to map the JavaBean properties to XML attributes or elements.
然后你必须得到一个将你的JavaBean树转换成XML文档的Marshaller。
And then you have to get a Marshaller which will transform your JavaBean tree into an XML document.
以下页面提供了有关这些步骤的简短教程。谷歌搜索JAXB将引导您获得更详尽的文档: http://www.vogella .de / articles / JAXB / article.html#jaxb
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
这篇关于在XML文件中构造事物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!