java dom parser only gets the first entity -
this code returs 1 "question " tag's element have 9 question element inside xml file.what wrong thing in here?do need loop.because when checked loop, loops 1 time.what problem?i figure out.
here xml:
<results> <question> <eno>3</eno> <qno>1</qno> <qtext>the battle of gettysburg fought during war?</qtext> <correctanswer>c</correctanswer> </question> <question> <eno>3</eno> <qno>2</qno> <qtext>neil armstrong , buzz aldrin walked how many minutes on moon in 1696?</qtext> <correctanswer>b</correctanswer> </question> </results>
my source code:
nodelist listofquestions = doc.getelementsbytagname("question"); for(int s=0; s<listofquestions.getlength(); s++) { system.out.println(listofquestions.getlength()); node firstquestionnode = listofquestions.item(0); if(firstquestionnode.getnodetype() == node.element_node){ element firstqelement = (element)firstquestionnode; nodelist enolist = firstqelement.getelementsbytagname("eno"); element enoelement =(element)enolist.item(s); nodelist enoslist = enoelement.getchildnodes(); string eno=((node)enoslist.item(s)).getnodevalue().trim(); system.out.println(eno); nodelist qnolist = firstqelement.getelementsbytagname("qno"); element qnoelement =(element)qnolist.item(s); nodelist qnoslist = qnoelement.getchildnodes(); string qno= ((node)qnoslist.item(s)).getnodevalue().trim(); system.out.println(qno); nodelist qtextlist = firstqelement.getelementsbytagname("qtext"); element qtextelement =(element)qtextlist.item(s); nodelist qtextslist = qtextelement.getchildnodes(); string qtext= ((node)qtextslist.item(s)).getnodevalue().trim(); system.out.println(qtext); nodelist correctanswerlist = firstqelement.getelementsbytagname("correctanswer"); element correctanswerelement =(element)correctanswerlist.item(s); nodelist correctanswerelementlist = correctanswerelement.getchildnodes(); string correctanswer= ((node)correctanswerelementlist.item(s)).getnodevalue().trim(); system.out.println(correctanswer); int i=st.executeupdate("insert question(eno,qno,qtext,correctanswer) values('"+eno+"','"+qno+"','"+qtext+"','"+correctanswer+"')"); system.out.println("s is"+s); } }
you have hardcoded
node firstquestionnode = listofquestions.item(0); ^^^
i think meant use variable s
there... or maybe not, it's hard tell you're trying do. regardless, there no other references listofquestions
, never retrieve node except first one.
Comments
Post a Comment