XML相较于txt有着许多优势。首先,它具有自我描述性;其次,它是结构化的,整个文档形成一个树形结构;而且,许多语言都对XML提供了支持,有相关的类库可以方便我们开发。
现在,我希望用xml存储一个问题(question)和对应的正确答案(answer),以及包含正确答案的四个选项内容,如下:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<question>鳞泷教给炭治郎的水之呼吸一共有多少种型?</question>
<answer>10</answer>
<options>
<A>8</A>
<B>9</B>
<C>10</C>
<D>11</D>
</options>
</root>
但是,这样的xml文档存在一个小问题:每一个选项都需要单独的几行代码来处理,无法实现遍历获取:
NodeList nodeListA = document.getElementsByTagName("A");
Node nodeA = nodeList.item(0).getFirstChild();
NodeList nodeListB = document.getElementsByTagName("B");
Node nodeB = nodeList.item(0).getFirstChild();
NodeList nodeListC = document.getElementsByTagName("C");
Node nodeC = nodeList.item(0).getFirstChild();
NodeList nodeListD = document.getElementsByTagName("D");
Node nodeD = nodeList.item(0).getFirstChild();
解决方法是,将XML文档中ABCD四个节点名更换为统一的option:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<question>鳞泷教给炭治郎的水之呼吸一共有多少种型?</question>
<answer>10</answer>
<options>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
</options>
</root>
这样,DAO类的代码中就可以实现用循环获取选项的数据了:
nodeList = document.getElementsByTagName("option");
for(int i=0;i<4;i++){
node = nodeList.item(i).getFirstChild();
options[i] = node.getNodeValue();
}
document.getElementsByTagName(标签名)函数获取的是XML文档中所有TagName与参数匹配的节点。如document.getElementsByTagName(“option”)就返回了一个包含四个节点的nodelist对象,通过对这个nodelist对象进行遍历操作,每一次遍历时获取其中一个节点的子节点(文本节点)的值并存储到options[]数组中,实现遍历获取数据。
如果你有更好的办法,可以实现遍历获取ABCD四个节点的数据,欢迎在评论区留言指教~
*以下附上QuestionxmlDAO类的源代码:
package DAO;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
@Component
public class QuestionxmlDAO {
private File file;
private String question;
private String answer;
private String options[] = new String[4];
//Getter and Setter
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public String[] getOptions() {
return options;
}
public void setOptions(String[] options) {
this.options = options;
}
//Constructors
public QuestionxmlDAO(){
this.file = new File("/root/Resources/DailyChallenge/question.xml");
}
public QuestionxmlDAO(String filename){
this.file = new File(filename);
}
public QuestionxmlDAO(File file){
this.file = file;
}
//执行该方法来对属性中的question、answer和options赋值
public Boolean ExtractData(){
try{
//创建DOM文档对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
Document document = documentBuilder.parse(file);
//获取各节点数据
//获取question
NodeList nodeList = document.getElementsByTagName("question");
Node node = nodeList.item(0).getFirstChild();
this.question = node.getNodeValue();
//获取answer
nodeList = document.getElementsByTagName("answer");
node = nodeList.item(0).getFirstChild();
this.answer = node.getNodeValue();
//获取options
nodeList = document.getElementsByTagName("option");
for(int i=0;i<4;i++){
node = nodeList.item(i).getFirstChild();
options[i] = node.getNodeValue();
}
return true;
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}