Get the href value using XPath/Hpple/Objective-C
Question
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!
OTHER TIPS
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"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow