TFHpple - iOS中的xpath - 让两个父母起来



想让两个父母(曾祖父母)或两个孩子下来?

<table style="background-color: #008000; border-style: none;" border="0" cellpadding="2" cellspacing="2">
  <tr>
    <td>
      <img height="5" width="5" border="0" src="https://spacer.gif" title="07:00,24hrs: B Shift /.../E704/RS704/Firefighter #2" alt="07:00,24hrs: B Shift /.../E704/RS704/Firefighter #2">
    </td>
  </tr>
</table>
<img height="2" width="2" border="0" src="https://spacer.gif" alt="">
  <img alt="" height="1" width="1" border="0" src="https://spacer.gif">
    </td>
    <TD ALIGN="RIGHT" VALIGN="TOP" width="17%">
      <a href="javascript:void(0)" title="01/15/2013" class="daylink" onClick="return DayClick('01/15/2013');">15</a>
    </TD>
    <td rowspan="2" width="5">
      <img alt="" height="1" width="1" border="0" src="https://spacer.gif">
    </td>
    </tr>
    <tr>
      <TD COLSPAN="2">
        <TABLE>
          <TR>
            <TD style="background-color: #41FFB9; " class="calexception">
              <a href="javascript:void(0)" onClick="return ShowRemoveExceptionWindow(&quot;4A30E80.fre01&quot;,&quot;3280530&quot;);" title="10hrs DetNonEMSStud(10),  07:00 - 17:00" style="color: #000000; text-decoration: none; font-weight: bold;">DetNonEMSStud(10)</a>
            </TD>
          </TR>
        </TABLE>

苹果:

NSString *tutorialsXpathQueryString = @"//table/tr/td";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
NSLog(@"here is url: %@", tutorialsNodes);
NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in tutorialsNodes) {
    Tutorial *tutorial = [[Tutorial alloc] init];
    [newTutorials addObject:tutorial];
    tutorial.url = [element objectForKey:@"style"];
    tutorial.title = [[element firstChild] objectForKey:@"title"];

我从<td>那里得到风格,从<a>那里得到标题就好了我还需要从<table>那里获得风格

我对 obj-c 很陌生,第一次尝试使用 XPATH,任何样本都会很棒!

我没有使用过TFHpple,但是如果它支持标准XPath,您应该能够使用此XPath从td转到其包含表:

ancestor::table[1]

此 XPath 选择作为上下文节点祖先的最近表,因此,如果您有以下标记:

<table>
    <tr><td></td></tr>
    <tr>
       <td>
         <table><tr><td></td></tr></table>
         <table>
             <tr>
                 <td>Hey!</td>
             </tr>
         </table>
       </td>
    </tr>      
</table>

您的上下文节点是带有文本"Hey!"的td,然后上面的 XPath 将选择第 6 行的表。

看起来 TFHpple 没有提供在上下文节点上评估 XPath 的方法。 鉴于此,一个新的建议 - 每个元素只有一个父元素,所以如果你继续向上浏览父元素,你最终应该找到表格。 向下要困难得多,因为一个元素可以有任意数量的直接子元素,每个子元素都有自己的子项集。我真的不了解 Objective-C,但如果你能确定表格只上了两级,那么这样的事情可能会起作用:

TFHppleElement *table = [[element parent] parent];

如果不能保证桌子是上两级的,那么应该有某种方法可以通过父级向上找到桌子。 这是伪代码,但希望你明白:

for (TFHppleElement *element in tutorialsNodes) {
    Tutorial *tutorial = [[Tutorial alloc] init];
    [newTutorials addObject:tutorial];
    tutorial.url = [element objectForKey:@"style"];
    tutorial.title = [[element firstChild] objectForKey:@"title"];
    TFHppleElement *table = [element parent];
    while(table != null && [table tagName] != "table") {
        table = [table parent]
    }
    // table should either be the parent table at this point, 
    //  or null if there was no parent table.

最新更新