zeja
Erfahrenes Mitglied
Also ich würde das mit JAXB machen:
Ergibt dann folgendes XML:
Java:
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
@XmlType
public class Answer {
@XmlAttribute(name = "correct", required = true)
private boolean correct = false;
@XmlValue
private String answerString;
public Answer(boolean correct, String answerString) {
super();
this.correct = correct;
this.answerString = answerString;
}
public Answer() {
}
@XmlTransient
public boolean isCorrect() {
return correct;
}
public void setCorrect(boolean correct) {
this.correct = correct;
}
@XmlTransient
public String getAnswerString() {
return answerString;
}
public void setAnswerString(String answerString) {
this.answerString = answerString;
}
}
Java:
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
@XmlType
public class Question {
public enum Difficulty {
EASY
}
@XmlAttribute(name = "schwierigkeit", required = true)
private Difficulty difficulty = Difficulty.EASY;
@XmlAttribute(name = "text", required = true)
private String questionString;
@XmlElement(name = "answers", required = true)
private List<Answer> answers = new ArrayList<Answer>();
public Question(Difficulty difficulty, String questionString) {
this.difficulty = difficulty;
this.questionString = questionString;
}
public Question() {
}
public void addAnswer(Answer answer) {
this.answers.add(answer);
}
@XmlTransient
public String getQuestionString() {
return questionString;
}
public void setQuestionString(String questionString) {
this.questionString = questionString;
}
@XmlTransient
public List<Answer> getAnswers() {
return answers;
}
public void setAnswers(List<Answer> answers) {
this.answers = answers;
}
@XmlTransient
public Difficulty getDifficulty() {
return difficulty;
}
public void setDifficulty(Difficulty difficulty) {
this.difficulty = difficulty;
}
}
Java:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@XmlRootElement(name = "quiz")
public class Quiz {
@XmlElement(name = "question", required = true)
private List<Question> questions = new ArrayList<Question>();
@XmlTransient
public List<Question> getQuestions() {
return questions;
}
public void setQuestions(List<Question> questions) {
this.questions = questions;
}
public void addQuestion(Question question) {
questions.add(question);
}
public static void main(String[] args) {
Quiz quiz = new Quiz();
Question question = new Question(Question.Difficulty.EASY, "Testfrage");
question.addAnswer(new Answer(true, "TestAntwort"));
question.addAnswer(new Answer(false, "TestAntwort2"));
quiz.addQuestion(question);
JAXBContext jc;
try {
jc = JAXBContext.newInstance(Quiz.class);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(quiz, new FileOutputStream("test.xml"));
Unmarshaller um = jc.createUnmarshaller();
Quiz quizRead = (Quiz) um.unmarshal(new File("test.xml"));
System.out.println(quizRead.getQuestions().toString());
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Ergibt dann folgendes XML:
Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<quiz>
<question text="Testfrage" schwierigkeit="EASY">
<answers correct="true">TestAntwort</answers>
<answers correct="false">TestAntwort2</answers>
</question>
</quiz>