我想要用JAVA读取一个XML文件到一个对象中,请教大神们!
<?xmlversion="1.0"encoding="gbk"?><rootversion="2.0"><file><no>...</no><name>...</nam...
<?xml version="1.0" encoding="gbk"?>
<root version="2.0">
<file>
<no>...</no>
<name>...</name>
<price>...</price>
<year>...</year>
<producer>...</producer>
</file>
</root> 展开
<root version="2.0">
<file>
<no>...</no>
<name>...</name>
<price>...</price>
<year>...</year>
<producer>...</producer>
</file>
</root> 展开
3个回答
展开全部
我知道有一个专门读取xml文件的jar包 demo4j.jar
我以前做过一些例子
<questions>
<question>
<content>
question1
</content>
<option>
long n = 999999999999;
</option>
<option>
int n = 999999999999L;
</option>
<option>
long n = 999999999999L;
</option>
<option>
double n = 999999999999;
</option>
<answer>
2;3
</answer>
<level>
2
</level>
</question>
<question>
<content>
question2
</content>
<option>
long n = 999999999999;
</option>
<option>
int n = 999999999999L;
</option>
<option>
long n = 999999999999L;
</option>
<option>
double n = 999999999999;
</option>
<answer>
2;3
</answer>
<level>
2
</level>
</question>
<question>
<content>
question3
</content>
<option>
long n = 999999999999;
</option>
<option>
int n = 999999999999L;
</option>
<option>
long n = 999999999999L;
</option>
<option>
double n = 999999999999;
</option>
<answer>
2;3
</answer>
<level>
2
</level>
</question>
</questions>
读取 :
private void initQuestion() {
try {
// 用于读取XML文件
SAXReader reader = new SAXReader();
// 用于表示整个的XML文档
Document doc = reader.read(new FileInputStream("d:/questions.xml"));
Element root = doc.getRootElement(); // questions
System.out.println(root.getName());
List<Element> list = root.elements("question");
for (Element qElement : list) {
Question question = new Question();
Element cElement = (Element) qElement.elements("content").get(0);
Element aElement = (Element) qElement.elements("answer").get(0);
Element lElement = (Element) qElement.elements("level").get(0);
List<Element> list2 = qElement.elements("option");
question.setContent(cElement.getText());
question.setAnswer(aElement.getText());
question.setLevel(lElement.getText());
for (Element oElement : list2) {
question.getOptions().add(oElement.getText());
}
questionList.add(question);
}
} catch (Exception e) {
e.printStackTrace();
}
}
比着做一下
我以前做过一些例子
<questions>
<question>
<content>
question1
</content>
<option>
long n = 999999999999;
</option>
<option>
int n = 999999999999L;
</option>
<option>
long n = 999999999999L;
</option>
<option>
double n = 999999999999;
</option>
<answer>
2;3
</answer>
<level>
2
</level>
</question>
<question>
<content>
question2
</content>
<option>
long n = 999999999999;
</option>
<option>
int n = 999999999999L;
</option>
<option>
long n = 999999999999L;
</option>
<option>
double n = 999999999999;
</option>
<answer>
2;3
</answer>
<level>
2
</level>
</question>
<question>
<content>
question3
</content>
<option>
long n = 999999999999;
</option>
<option>
int n = 999999999999L;
</option>
<option>
long n = 999999999999L;
</option>
<option>
double n = 999999999999;
</option>
<answer>
2;3
</answer>
<level>
2
</level>
</question>
</questions>
读取 :
private void initQuestion() {
try {
// 用于读取XML文件
SAXReader reader = new SAXReader();
// 用于表示整个的XML文档
Document doc = reader.read(new FileInputStream("d:/questions.xml"));
Element root = doc.getRootElement(); // questions
System.out.println(root.getName());
List<Element> list = root.elements("question");
for (Element qElement : list) {
Question question = new Question();
Element cElement = (Element) qElement.elements("content").get(0);
Element aElement = (Element) qElement.elements("answer").get(0);
Element lElement = (Element) qElement.elements("level").get(0);
List<Element> list2 = qElement.elements("option");
question.setContent(cElement.getText());
question.setAnswer(aElement.getText());
question.setLevel(lElement.getText());
for (Element oElement : list2) {
question.getOptions().add(oElement.getText());
}
questionList.add(question);
}
} catch (Exception e) {
e.printStackTrace();
}
}
比着做一下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询