Question

trying to get the value of the element "a "and "span ". Using HTMLCleaner.

<div class="info">
  <p class="name">
    <a href="http://www.zxdv.com/level/1/film/616/sr/1/">Tron</a> 
    <span class="year">2001</span>
  </p>
</div>

here is the code:

TagNode linkElements[] = rootNode.getElementsByName("div", true);
int s=0;
for (int i = 0; linkElements != null && i < linkElements.length; i++)
{
    if (linkElements[i].getAttributes().toString().equals("{class=info}")) {
        TagNode linkElements2[] = linkElements[i].getElementsByName("p", true);
        for (int i2 = 0; linkElements2 != null && i2 < linkElements2.length; i2++)
        {
            TagNode linkElements3[] = linkElements2[i2].getElementsByName("a", true);
            TagNode linkElements4[] = linkElements2[i2].getElementsByName("span", true);
            for (int i3 = 0; linkElements3 != null && i3 < linkElements3.length; i3++)
            {
                if (s <= 20) {
                    String currentlink = linkElements3[i3].getText().toString();
                    String currentlink2 = linkElements4[i3].getText().toString();
                    slink[s] = currentlink+"\n"+currentlink2;
                    s++;
                }   
            }   
        }
    }
}

as i understand a take first "div" element then then his child element "p", but when i taling "a" and "span" elements values return emptyю prompt please where I make mistake. thanks

Was it helpful?

Solution

Reduce work by using XPath instead

TagNode root = htmlCleaner.clean(url);
// Xpath to 'a'
Object[] foundList = root.evaluateXPath("//div/p[@class='name']/a");
if(foundList == null || foundList.length < 1) {
    return;
}

TagNode aNode = (TagNode)foundList[0];
String aNodeTextContent = aNode.getText();

// Xpath to 'span'
foundList = root.evaluateXPath("//div/p[@class='name']/span");
if(foundList == null || foundList.length < 1) {
    return;
}

TagNode spanNode = (TagNode)foundList[0];
String spanNodeTextContent = spanNode.getText();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top