Ahmet Özrahat Ahmet Özrahat -4 years ago 96
Android Question

Android XML Reader Reads Once

i just tried to make a XML Reader but i had some troubles. When I parse the XML file it says there is just one tag and read just this one. But there is more than one. Here is my code:

@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 File:

<?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>




If I write
NodeList sorular = document.getElementsByTagName("sorular");
it says count of
NodeList = 1
but if I write
NodeList sorular = document.getElementsByTagName("soru");
it says the
count = 2
but it gives me a NullPointerException

Answer Source

because siraElement.getChildNodes().getLength() will always return 1 element so when

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

so the first node will be at 0 index but when i=1 then will be no Node at index 1

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

so you can fetch the always from 0 index

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

plus do the same for all values like below

textElement.getChildNodes().item(0).getNodeValue()
...
dogruElement.getChildNodes().item(0).getNodeValue().trim()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download