I have stored this into NodeList list. When I write :
Node n = (Node)list.elementAt(1);
System.out.println (n.toHtml ());
It prints:
<td height="5">Togo</td>
So upto this, it is perfect. But whenever I write
Node m = n.getPreviousSibling ();
System.out.println (m.toHtml ());
I was expecting it should print : <td height="5">Mongolia</td> ; but it prints nothing. Same is the case for getNextSibling() method.
Can anyone suggest whether I am making any mistake? What is the way to print as I wish?
Shuvadeep
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
All nodes (even whitespace text) are in the children list. I believe you are seeing nothing because you are printing out the TextNode containing "\n " bewteen the Mongolia and Togo table column elements.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thank you. It does print <td height="5">Mongolia</td> properly now.
Just a small question. Suppose my NodeList only contain <td height="5">Togo</td>. Could it print the same <td height="5">Mongolia</td> by using getPreviousSibling() method?
Shuvadeep
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
It seems getPreviousSibling() and getNextSibling() methos have been written for convenience. Now I have a very simple web-page like this :
<html>
<body>
<table>
<tr>
<td height="5">Mongolia</td>
<td height="5">Togo</td>
<td height="5">Uruguay</td>
</tr>
</table>
</body>
</html>
I have picked a NodeList for all "TD" having height=5 like below:
try
{ return parser.extractAllNodesThatMatch(new AndFilter(new TagNameFilter("TD"),new HasAttributeFilter("height","5")));
}catch(ParserException e)
{
System.out.println("Parsing error "+e);
e.printStackTrace ();
return null;
}
I have stored this into NodeList list. When I write :
Node n = (Node)list.elementAt(1);
System.out.println (n.toHtml ());
It prints:
<td height="5">Togo</td>
So upto this, it is perfect. But whenever I write
Node m = n.getPreviousSibling ();
System.out.println (m.toHtml ());
I was expecting it should print : <td height="5">Mongolia</td> ; but it prints nothing. Same is the case for getNextSibling() method.
Can anyone suggest whether I am making any mistake? What is the way to print as I wish?
Shuvadeep
All nodes (even whitespace text) are in the children list. I believe you are seeing nothing because you are printing out the TextNode containing "\n " bewteen the Mongolia and Togo table column elements.
Thank you. It does print <td height="5">Mongolia</td> properly now.
Just a small question. Suppose my NodeList only contain <td height="5">Togo</td>. Could it print the same <td height="5">Mongolia</td> by using getPreviousSibling() method?
Shuvadeep
Yes. Items in the nodelist are still fully linked via the children lists.
You could have just tried it, no?