0

Currently I'm following Ray Wenderlich's tutorial dealing with parsing html on ios. Everything is explained perfectly and according to this tree:

enter image description here
(source: raywenderlich.com)

they extract the title and url tag for each tutorial.

This is how they create an array to hold their Tutorial object:

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

        // 6
        tutorial.title = [[element firstChild] content];

        // 7
        tutorial.url = [element objectForKey:@"href"];
    }

Now here is my question. I'm currently trying this out on my site. The problem is that I don't know how to get the 4th child of every <tr> tag.

Here is my html tree.

enter image description here

I'm trying to only get that fourth child from every tag. But I don't know how to approach it.

Would this be correct?

        // 6
        tutorial.title = [[element firstChild] content];

        // 7
        tutorial.amount = [[element fourthChild] objectForKey:@"td"];
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
KingPolygon
  • 4,753
  • 7
  • 43
  • 72

1 Answers1

0

You can use children property to do so :)

First you need to reach out to required tag to get all in element children. To do so you can set xpath query like this one: div[@id='main']/table[@class='bodytext']/tbody/tr

Then you can get the fourth child using children property. Here is the sample code.

//3
NSString *tutorialsXpathQueryString = @"//div[@id='main']/table[@class='bodytext']/tbody/tr";
NSArray *tutorialsNodes = [tutorialsParser searchWithXPathQuery:tutorialsXpathQueryString];

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

// 6
TFHppleElement *trTag = [[element children] objectAtIndex:3];

tutorial.title = [trTag content];

Hope this helps

iMemon
  • 1,095
  • 1
  • 12
  • 21