开发者

iPhone SDK: parsing html table and displaying in iphone tableview

开发者 https://www.devze.com 2023-04-12 12:04 出处:网络
I am using the hpple plugin to parse and display html elements in my iphone app. The problem i\'m having is say there is a table on the webpage I am trying to parse, with several rows, (that ma开发者_

I am using the hpple plugin to parse and display html elements in my iphone app. The problem i'm having is say there is a table on the webpage I am trying to parse, with several rows, (that ma开发者_如何学Pythony change from time to time). How do I go about getting the number of rows in this table, and iterating through each row and getting the different text content on each row. Is this possible with the hpple plugin?

Thanks in advance.


If all that want to do is count the rows in a table with class "tableClass" then you might try something like this

 // Count the number of rows in a particular table
 NSString *searchString = [NSString stringWithFormat:@"//table[@class='tableClass']/tr;
 NSArray *tableRows  = [xpathParser search:searchString];
 NSInteger rows = [tableRows count];
 NSLog(@"There are %d table Rows", rows);

 // For loop to step through the rows
 for(int j = 1; j <= rows; j++) {
      searchString = [NSString stringWithFormat:@"//table[@class='tableClass']/tr[%d]/td", j];
      NSArray *tableCells  = [xpathParser search:searchString];
 }

This should step through the table row by row and each time scrape the individual data cells (I didn't test it so there are no guarantees.) The problem with this is that it will be very slow if your table has more than a few rows.

You are better off just calling the table and scraping all the td cells at once from the table and then deciding how they make up the rows. This way is easy if the number of cells in a row stays constant.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号