dom - How to fetch the child nodes of repeated elements in java for nodelist -
i need fetch value of name
element child of e
element:
<a> <b> <c> <d> <e><name>123</name></e> <e><name>456</name></e> <e><name>456</name></e> </d> </c> </b> </a>
this code:
nodelist lineitemattributechildrenlist = doc.getelementsbytagname("e").item(0).getchildnodes(); if(lineitemattributechildrenlist != null && lineitemattributechildrenlist.getlength() > 0) { system.out.println("inside if , checking length" + lineitemattributechildrenlist.getlength()); (int = 0; < lineitemattributechildrenlist.getlength(); i++) { system.out.println("i " + i); system.out.println("inside for"); system.out.println("name==============" + lineitemattributechildrenlist.item(i).getnodename()); system.out.println("value==============" + lineitemattributechildrenlist.item(i).gettextcontent()); } }
from above code first inner element name value e
element, remaining 2 not able values. not going second e
element in loop.
you getting 0th element of nodelist of "e" elements.
nodelist lineitemattributechildrenlist = doc.getelementsbytagname("e").item(0).getchildnodes(); ... (int = 0; < lineitemattributechildrenlist.getlength(); i++) {
you should iterate through nodelist instead of children of first element. here how new code look:
nodelist lineitemattributechildrenlist = doc.getelementsbytagname("e"); if (lineitemattributechildrenlist != null && lineitemattributechildrenlist.getlength() > 0) { system.out.println("inside if , checking length"+lineitemattributechildrenlist.getlength()); (int = 0; < lineitemattributechildrenlist.getlength(); i++) { system.out.println("i "+i); system.out.println("inside for"); system.out.println("name=============="+lineitemattributechildrenlist.item(i).getnodename()); system.out.println("value=============="+lineitemattributechildrenlist.item(i).gettextcontent()); } }
Comments
Post a Comment