gpt4 book ai didi

java - Android XML Reader 读取一次

转载 作者:行者123 更新时间:2023-11-29 19:18:35 25 4
gpt4 key购买 nike

我只是想制作一个 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

最佳答案

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

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

所以第一个节点将位于 0 索引处,但是当 i=1 时,索引 1< 处将没有 Node/

siraElement.getChildNodes().item(i).getNodeValue() //i=1  error

所以你可以从 0 索引中获取 always

 int sira = Integer.parseInt(siraElement.getChildNodes().item(0).getNodeValue());
// ^^

对下面的所有值做同样的事情

textElement.getChildNodes().item(0).getNodeValue()
...
dogruElement.getChildNodes().item(0).getNodeValue().trim()

关于java - Android XML Reader 读取一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42909324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com