Search code examples
htmlxpathhrefhpple

Get the href value using XPath/Hpple/Objective-C


I tried everything that makes (and doesn't make) sense for me. I have the following html code which I try to parse with XPath in Objective-C:

<tr style="background-color: #eaeaea">
   <td class="content">
      <a href="index.php?cmd=search&id=foo">bar</a>
   </td>
</tr>

I get the "bar" via //tr/td[@class='content']/a/text().

But I have no idea how to get the index.php?cmd=search&id=foo.

It really drives me to despair :-(

Thank you so much for your help!


Solution

  • After trying the whole night (again) I found out how to solve my problem:

    //Put the elements in an array
    NSArray *someArray = [xpathParser searchWithXPathQuery:@"//tr/td[@class='content']/a"];
    
    //Just one possibility to make a for-loop
    for (TFHppleElement *element in someArray) {
        //This is the important line:
        NSString *myURL = [[element attributes] objectForKey:@"href"];
        //Do whatever you want with myURL
    }
    

    I hope that helps some folks out there!