TFHpple提取元素超过'firstChild'或'parent'

时间:2013-01-28 19:02:50

标签: ios objective-c xpath hpple

如何在阵列下方获取元素,而不是firstChildparent

NSString *tutorialsXpathQueryString = @"//table/tr/td/ancestor::table[1]";
//  @"//tr/td/table/tr/td/a"

NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];
NSLog(@"here is url: %@", tutorialsNodes);

NSMutableArray *newTutorials = [[NSMutableArray alloc] initWithCapacity:0];
for (TFHppleElement *element in tutorialsNodes) {
    // 5
    Tutorial *tutorial = [[Tutorial alloc] init];
    [newTutorials addObject:tutorial];

    tutorial.url = [[element parent] objectForKey:@"style"];

    //tutorial.url = [element objectForKey:@"style"];

    //tutorial.url = [[element firstChild] objectForKey:@"style"];

    //tutorial.url = [element tagName];


    tutorial.title = [[element parent] objectForKey:@"title"];

    //tutorial.title = [element objectForKey:@"title"];

    //tutorial.title = [[element firstChild] objectForKey:@"title"];

    //tutorial.title = [[element firstChild] tagName];

我的数组样本:

{\n                   nodeAttributeArray =                                                          (\n         {\n           attributeName = style;\n                                                             nodeContent = \"background-color: #008000; border-style: none;\";\n                                                          },\n                                                                                                                 {\n                                                            attributeName = border;\n                                                              nodeContent = 0;\n                                                        },\n                                                                                                                  {\n                                                            attributeName = cellpadding;\n                                                              nodeContent = 2;\n                                                        },\n                                                                                                                  {\n                                                            attributeName = cellspacing;\n                                                              nodeContent = 2;\n                                                        }\n                                                      );\n                                                    nodeChildArray =                                                       (\n                                                                                                                   {\n                                                            nodeChildArray =                                                              (\n                                                                                                                                 {\n                                                                    nodeChildArray =                                                                      (\n            },\n                                                                                                                                                                {\n                                                                                    attributeName = border;\n                                                                                    nodeContent = 0;\n                                                                                },\n                                                                                                                                                                {\n                                                                                    attributeName = src;\n                                                                                    nodeContent = \"https://spacer.gif\";\n                                                                                },\n                                                                                                                                                                {\n                                                                                    attributeName = title;\n                                                                                    nodeContent = \"07:00,24hrs: B Shift /.../E704/RS704/Firefighter #2\";\n                                                                                },\n                                                                                                                                                                {\n                                                                                    attributeName = alt;\n                                                                                    nodeContent = \"07:00,24hrs: B Shift /.../E704/RS704/Firefighter #2\";\n                                                                                }\n                                                                            );\n                                                                            nodeName = img;\n                                                                        }\n                                                                    );\n                                                                    nodeName = td;\n                                                                },\n                                                                                                                                

1 个答案:

答案 0 :(得分:0)

我对Objective-C没有经验,但请试一试。

TFHppleElement的parent属性本身就是一个TFHppleElement,因此它又有一个parent。如果你继续通过这些父母,你最终应该找到你正在寻找的节点。例如,让父母2级升级:

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

由于你的XPath只会找到桌面下两个级别的td(事实上,这是他们应该唯一的位置),然后检索父级的父级应该会找到你正在寻找的表格