1

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!

Siva Charan
  • 17,940
  • 9
  • 60
  • 95
HAS
  • 19,140
  • 6
  • 31
  • 53

2 Answers2

2

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!

HAS
  • 19,140
  • 6
  • 31
  • 53
1

To get href part via XPATH, you need to use @ symbol before the attribute name.

//tr/td[@class='content']/a/@href

UPDATED:

It seems, you are referring to iphone.

Try something like this for getting attribute value

NSArray *link = [xmlData valueForKey:@"href"];
Siva Charan
  • 17,940
  • 9
  • 60
  • 95
  • Thanks for the fast answer, but I tried that and it doesn't work... I don't know, I store the values it finds in an NSArray, and if I use "//tr/td[@class='content']/a/text()" everything's just fine but when I change it to "//tr/td[@class='content']/a/@href" Xcode says "*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil" so XPath didn't found anything, did it? – HAS Jun 28 '12 at 23:03
  • Thank you so much for your help, after spending another night I found a way. If there's another (better) way let me know! – HAS Jun 29 '12 at 08:50
  • 1
    HAS - could you mark your answer as correct? I too found that objectForKey:@"href" was the solution. – Carl Jan 08 '14 at 17:56