0

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                                                                                                                                
Vitaly S.
  • 2,389
  • 26
  • 40
BarclayVision
  • 865
  • 2
  • 12
  • 41

1 Answers1

0

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.

JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • actually its just the opposite, I can no longer get style and title from the td but I can now get the upper table in question. the td elements seem further down the array... like second or third child... but I don't know how to get elements that deep from the objectForKey: – BarclayVision Jan 28 '13 at 19:25
  • @BarclayVision What I'm suggesting is that you go back to your original XPath (`//table/tr/td`) to locate all of the `td`s. Once you have the `td`s, I suspect you can use an approach like the one I'm describing above to locate their parent table. There's no use in using `//table/tr/td/ancestor::table[1]` all in a single XPath. That's just a very roundabout way to get the same result as `//table[tr/td]` that is, any table that has a `td` under a `tr`. – JLRishe Jan 28 '13 at 19:32
  • What was looking for was the multi levels deep, got by doing: '[[[element parent] parent] parent]' – BarclayVision Jan 30 '13 at 13:08