getNextSibling() never ends? DOM XML nodes (org.w3c.dom)

A

Alan

It appears that the method getNextSibling() in the Node
(org.w3c.dom) interface never returns null. I cannot find what I am
doing wrong. The code below is an infinite loop, although I think I
am using the stopping criteria described for the method (returns null
if no next sibling exists).

What is wrong here? I have provided my method (after being
distilled down and still does not work) and the output I`m seeing. I
am not sure why it always shows a name for the next sibling node, when
it`s only 2-3 nodes deep.

Thanks in advance for any pointers you can give me. Alan

private static Node updateSibling(Node node)
{
Node sibling = node.getNextSibling();
// while there is another sibling
// but the one searching for has not been found
int count = 0;
while (sibling != null)
{
count++;
System.out.println(" count = " + count);
System.out.println("Sibling = " + sibling);
sibling = node.getNextSibling();
}
return node;
}

Output:
count = 1
Sibling = [w:r: null]
count = 2
Sibling = [w:r: null]
count = 3
Sibling = [w:r: null]
count = 4
Sibling = [w:r: null]
count = 5
Sibling = [w:r: null]
count = 6
Sibling = [w:r: null]
count = 7
Sibling = [w:r: null]
count = 8
Sibling = [w:r: null]
count = 9
Sibling = [w:r: null]
count = 10
Sibling = [w:r: null]
. . .
count = 22909
Sibling = [w:r: null]
.. . .
 
A

Arne Vajhøj

Alan said:
It appears that the method getNextSibling() in the Node
(org.w3c.dom) interface never returns null. I cannot find what I am
doing wrong. The code below is an infinite loop, although I think I
am using the stopping criteria described for the method (returns null
if no next sibling exists).

What is wrong here? I have provided my method (after being
distilled down and still does not work) and the output I`m seeing. I
am not sure why it always shows a name for the next sibling node, when
it`s only 2-3 nodes deep.
private static Node updateSibling(Node node)
{
Node sibling = node.getNextSibling();
// while there is another sibling
// but the one searching for has not been found
int count = 0;
while (sibling != null)
{
count++;
System.out.println(" count = " + count);
System.out.println("Sibling = " + sibling);
sibling = node.getNextSibling();
}
return node;
}

Output:
count = 1
Sibling = [w:r: null]
count = 22909
Sibling = [w:r: null]

Can you create a full example showing the problem.

I can not.

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

public class SiblingProblem {
public static void main(String[] args) throws Exception {
String xml =
"<all><rec><a>1</a><b>2</b></rec><rec><a>2</a><b>4</b></rec></all>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(xml)));
Node n = doc.getDocumentElement();
while(n != null) {
System.out.println(n);
n = n.getNextSibling();
}
System.out.println("====");
n = doc.getDocumentElement().getFirstChild();
while(n != null) {
System.out.println(n);
n = n.getNextSibling();
}
System.out.println("====");
n = doc.getDocumentElement().getFirstChild().getFirstChild();
while(n != null) {
System.out.println(n);
n = n.getNextSibling();
}
}
}

outputs:

[all: null]
====
[rec: null]
[rec: null]
====
[a: null]
[b: null]

which looks correct to me.

Arne
 
P

Patricia Shanahan

Alan said:
It appears that the method getNextSibling() in the Node
(org.w3c.dom) interface never returns null. I cannot find what I am
doing wrong. The code below is an infinite loop, although I think I
am using the stopping criteria described for the method (returns null
if no next sibling exists).

What is wrong here? I have provided my method (after being
distilled down and still does not work) and the output I`m seeing. I
am not sure why it always shows a name for the next sibling node, when
it`s only 2-3 nodes deep.

Thanks in advance for any pointers you can give me. Alan

private static Node updateSibling(Node node)
{
Node sibling = node.getNextSibling();
// while there is another sibling
// but the one searching for has not been found
int count = 0;
while (sibling != null)
{
count++;
System.out.println(" count = " + count);
System.out.println("Sibling = " + sibling);
sibling = node.getNextSibling();
}
return node;
}

Warning: I have never used this interface - the following is based
entirely on my reading of the API documentation.

You always call the getNextSibling method for the same node, so you
should always get the result. Perhaps you should be doing:

sibling = sibling.getNextSibling();

inside the loop to advance through the siblings.

Patricia
 
M

Mike Schilling

Patricia said:
Warning: I have never used this interface - the following is based
entirely on my reading of the API documentation.

You always call the getNextSibling method for the same node, so you
should always get the result. Perhaps you should be doing:

sibling = sibling.getNextSibling();

inside the loop to advance through the siblings.

Patricia is (as usual) correct.
 
A

Arne Vajhøj

Patricia said:
Warning: I have never used this interface - the following is based
entirely on my reading of the API documentation.

You always call the getNextSibling method for the same node, so you
should always get the result. Perhaps you should be doing:

sibling = sibling.getNextSibling();

inside the loop to advance through the siblings.

Yuck. Good seen.

Arne
 
G

GArlington

   It appears that the method getNextSibling() in the Node
(org.w3c.dom) interface never returns null.  I cannot find what I am
doing wrong.  The code below is an infinite loop, although I think I
am using the stopping criteria described for the method (returns null
if no next sibling exists).

     What is wrong here?  I have provided my method (after being
distilled down and still does not work) and the output I`m seeing.   I
am not sure why it always shows a name for the next sibling node, when
it`s only 2-3 nodes deep.

     Thanks in advance for any pointers you can give me.        Alan

    private static Node updateSibling(Node node)
    {
        Node sibling = node.getNextSibling();
getNextSibling() returns The node immediately following this node. If
there is no such node, this returns null
Your current (this) node here is "node"
        // while there is another sibling
        // but the one searching for has not been found
        int count = 0;
        while (sibling != null)
        {
            count++;
            System.out.println("   count = " + count);
            System.out.println("Sibling = " + sibling);
Your current (this) node here is STILL "node", it never changed since
the last call to getNextSibling()...
So, what do you expect getNextSibling() to return???
            sibling = node.getNextSibling();
        }
        return node;
    }
<snap>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top