Android XML Reader只读一次

时间:2017-03-20 16:46:32

标签: java android xml

我只是想制作一个XML阅读器,但我遇到了一些麻烦。当我解析XML文件时,它说只有一个标签,只读这个标签。但不止一个。这是我的代码:

    @Override
    protected Void doInBackground(Void... voids) {

        try {
            URL url = new URL(QuizActivity.fileUrl);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();

            Document document = builder.parse(new InputSource(url.openStream()));
            document.getDocumentElement().normalize();

            NodeList sorular = document.getElementsByTagName("sorular");
            Log.d("Soru Sayısı", String.valueOf(sorular.getLength()));
            for(int i = 0; i < sorular.getLength(); i++){
                Node sorularNode = sorular.item(i);
                Element sorularElement = (Element) sorularNode;

                NodeList siraNodeList = document.getElementsByTagName("sira");
                Element siraElement = (Element) siraNodeList.item(i);

                NodeList textNodeList = document.getElementsByTagName("text");
                Element textElement = (Element) textNodeList.item(i);

                NodeList aNodeList = document.getElementsByTagName("a");
                Element aElement = (Element) aNodeList.item(i);

                NodeList bNodeList = document.getElementsByTagName("b");
                Element bElement = (Element) bNodeList.item(i);

                NodeList cNodeList = document.getElementsByTagName("c");
                Element cElement = (Element) cNodeList.item(i);

                NodeList dNodeList = document.getElementsByTagName("d");
                Element dElement = (Element) dNodeList.item(i);

                NodeList eNodeList = document.getElementsByTagName("e");
                Element eElement = (Element) eNodeList.item(i);

                NodeList dogruNodeList = document.getElementsByTagName("dogru");
                Element dogruElement = (Element) dogruNodeList.item(i);

                int sira = Integer.parseInt(siraElement.getChildNodes().item(i).getNodeValue());

                SinavActivity.soruText.put(sira, textElement.getChildNodes().item(i).getNodeValue());
                SinavActivity.cevap_a.put(sira, aElement.getChildNodes().item(i).getNodeValue());
                SinavActivity.cevap_b.put(sira, bElement.getChildNodes().item(i).getNodeValue());
                SinavActivity.cevap_c.put(sira, cElement.getChildNodes().item(i).getNodeValue());
                SinavActivity.cevap_d.put(sira, dElement.getChildNodes().item(i).getNodeValue());
                SinavActivity.cevap_e.put(sira, eElement.getChildNodes().item(i).getNodeValue());
                SinavActivity.dogruCevap.put(sira, dogruElement.getChildNodes().item(i).getNodeValue().trim());

            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return null;
    }

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<sorular>
<Matematik>
    <soru>
        <sira>1</sira>
        <text>Bir sınıftaki öğrencilerin % 40 ı kız öğrencidir. Kız öğrencilerin % 90 ı, erkek öğrencilerin %80 matematik dersinden başarılıdır. Buna göre, bu sınıfın matematik dersinden başarısı yüzde kaçtır?</text>
        <a>84</a>
        <b>46</b>
        <c>20</c>
        <d>12</d>
        <e>50</e>
        <dogru>A</dogru>
    </soru>
    <soru>
        <sira>2</sira>
        <text>A ve B boş olmayan 2 kümedir 2.s(A)=3.s(B)= 5.s(A ∩ B) olduğuna göre s(A-B) en az kaçtır?</text>
        <a>8</a>
        <b>10</b>
        <c>2</c>
        <d>9</d>
        <e>12</e>
        <dogru>D</dogru>
    </soru>
</Matematik>

如果我写NodeList sorular = document.getElementsByTagName("sorular");它会说NodeList = 1,但如果我写NodeList sorular = document.getElementsByTagName("soru");它会说count = 2,但它会给我一个NullPointerException

2 个答案:

答案 0 :(得分:1)

因为siraElement.getChildNodes().getLength()将始终返回1元素,所以当

siraElement.getChildNodes().item(i).getNodeValue() //i=0

因此,第一个节点将位于0索引,但是i=1时,Node

} <{1}}

1

因此您可以从siraElement.getChildNodes().item(i).getNodeValue() //i=1 error 索引

获取始终
0

加上对所有值(例如下面的

)执行相同的操作
 int sira = Integer.parseInt(siraElement.getChildNodes().item(0).getNodeValue());
//                                                           ^^

答案 1 :(得分:1)

正如Pavneet_Singh所说,我编辑了这里是代码:

    @Override
    protected Void doInBackground(Void... voids) {

        try {
            URL url = new URL(QuizActivity.fileUrl);
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();

            Document document = builder.parse(new InputSource(url.openStream()));
            Element rootElement = document.getDocumentElement();

            NodeList sorular = rootElement.getElementsByTagName("soru");
            for(int i = 0; i < sorular.getLength(); i++){
                Node soru = sorular.item(i);
                Element soruElement = (Element) soru;
                String siraText = soruElement.getElementsByTagName("sira").item(0).getTextContent();
                String text = soruElement.getElementsByTagName("text").item(0).getTextContent();
                String a = soruElement.getElementsByTagName("a").item(0).getTextContent();
                String b = soruElement.getElementsByTagName("b").item(0).getTextContent();
                String c = soruElement.getElementsByTagName("c").item(0).getTextContent();
                String d = soruElement.getElementsByTagName("d").item(0).getTextContent();
                String e = soruElement.getElementsByTagName("e").item(0).getTextContent();
                String dogru = soruElement.getElementsByTagName("dogru").item(0).getTextContent();

                int sira = Integer.parseInt(siraText);
                SinavActivity.soruText.put(sira, text);
                SinavActivity.cevap_a.put(sira, a);
                SinavActivity.cevap_b.put(sira, b);
                SinavActivity.cevap_c.put(sira, c);
                SinavActivity.cevap_d.put(sira, d);
                SinavActivity.cevap_e.put(sira, e);
                SinavActivity.dogruCevap.put(sira, dogru);
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return null;
    }