I am trying to process only the elements of type <a> in a list:
int i = 0;
for (Element el : els) {
i++;
Log.w(TAG, "--------------------------");
Log.w(TAG, "el(" + i + "): " + el.html());
if (el.tagName().contentEquals("a")) {
Log.d(TAG, "<a> only! el(" + i + "): " + el.html());
}
}
But the LogCat output only shows the Log.w() ones, not the the Log.d(), although I can see clearly in the Log.w() output that there are elements of type <a>:
--------------------------
el(1):
--------------------------
el(2): <a href="http://www.ddd.tld/article/ar01.html?mod=Header"><em>Subject1:</em> One line of text </a>
--------------------------
el(3):
--------------------------
el(4):
--------------------------
el(5): <a href="http://www.ddd.tld/article/ar02.html?mod=Header"><em>Subject Two:</em> Another line of text </a>
--------------------------
el(6):
--------------------------
el(7):
--------------------------
el(8): <img src="images/sub_key.gif" /> <a href="http://www.ddd.tld/article/ar03.html?mod=Header"><em>Subject Three:</em> Yet another line of text </a>
--------------------------
el(9):
--------------------------
el(10):
What am I missing? What am I doing wrong?
The correct way to select all elements with an
<a>tag would be to use a CSS selector. You can use this on anElementsobject. To convert yourList<Element>to anElementsyou simply use theElements(Element... elements)constructor like so:From this, you can use the CSS Selectors to extract the
Elements like so:You can then iterate through these and do as you wish with them.