NodeList.item(i) returns every other child node

I have used the following code to eliminate null elements from a Node.

But somehow NodeList.item(i) only returns every other child node.

The original Node (retNode) is:

<getHearingDetailsByCaseNoWSResponse><hearingDetailsVO xmlns="http://vo.cis.com"><NOA/><applicationNo>sAppNo0</applicationNo><caseNo/><designation/><docID/><duration/><endTime/><hearDate/><judgeName/><natureOfClaim/><outCome/><outComeTime/><outcomeDate/><remarks/><roomNo/><sessionNo/><stEndTime/><tohCode/><typeHear/></hearingDetailsVO></getHearingDetailsByCaseNoWSResponse>

retNode.getLength() gives 19 which is correct.

NodeList lv2List = retNode.getChildNodes();

int numOfLv2 = lv2List.getLength();

if(numOfLv2 == 0){

if(!hasAttribute(node)) parent.removeChild(node);

return;

}

for(int i=0; i<numOfLv2; i++){

Node lv2Node = lv2List.item(i);

if(lv2Node ==null || lv2Node.getNodeType() != Node.ELEMENT_NODE)continue;

removeNullElement(lv2Node);

}

However, lv2List.item(0) returns ><NOA/> element, lv2List.item(0) returns <caseNo/>, lv2List.item(1) returns <docID/>, and lv2List.item(9) returns <typeHear/>. From item(10) to item(18) all returns null.

I was very confused by this.

I am using Xerces-J implementation. I have set

System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");

Did I do something wrong or I missed out something?

Please enlighten me.

Regards,

Xinjun

[2136 byte] By [Andrew_sga] at [2007-11-26 15:06:33]
# 1

When an element is removed from a list, the index of each element coming after the removed one will be one less than it was before the removal, so you will have to subtract 1from the variable "i" after you have removed an element and before you start the next cycle in order not to skip the index of the removed element:for(int i=0; i<numOfLv2; i++) {

Node lv2Node = lv2List.item(i);

if(lv2Node == null || lv2Node.getNodeType() != Node.ELEMENT_NODE) continue;

removeNullElement(lv2Node);

i--

}

>

prgguya at 2007-7-8 8:56:30 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
My program cannot terminate after this modification. The program keeps trying to remove elements from <applicationNo>.
Andrew_sga at 2007-7-8 8:56:30 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3

But your comments remind me of adjusting the index.

I modified the removeNullElement(Node) method so that it returns a boolean value.

if(removeNullElement(lv2Node) == true) {

i--;

}

Thank you, prgguy.

Andrew_sga at 2007-7-8 8:56:30 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...