使用XML技术实现遍历获取数据

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;
    }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值