Question

How can I get elements further down the array than firstChild or parent

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];

sample of my array:

{\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                                                                                                                                
Was it helpful?

Solution

I'm not experienced with Objective-C, but please give this a try.

The TFHppleElement's parent property is itself a TFHppleElement, so it in turn has a parent. If you keep going up through these parents, you should eventually find the node you're looking for. For example to get a parent 2 levels up:

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

Since your XPath will only locate tds that are two levels under a table (and indeed, that's the only place they should be), then retrieving the parent's parent should get you the table you're looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top