1

我尝试了一切对我有意义(和没有意义)的东西。我有以下 html 代码,我尝试在 Objective-C 中使用 XPath 进行解析:

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

我得到了"bar"via //tr/td[@class='content']/a/text()

但我不知道如何获得index.php?cmd=search&id=foo.

这真的让我绝望:-(

非常感谢你的帮助!

4

2 回答 2

2

在尝试了整个晚上(再次)之后,我发现了如何解决我的问题:

//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
}

我希望这对那里的一些人有所帮助!

于 2012-06-29T08:47:08.403 回答
1

要通过 XPATH 获取 href 部分,您需要@在属性名称前使用符号。

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

更新:

看来,你指的是iphone。

尝试这样的事情来获取属性值

NSArray *link = [xmlData valueForKey:@"href"];
于 2012-06-28T22:55:06.903 回答