Reputation: 55
I am trying to make a UITableView based application for the iPhone. I have done this before by extracting data from RSS feeds, but for this application I am trying to use data from a webpage. The data is in a web table format (http://www.roblox.com/Forum/Default.aspx), and has different hierarchies because it is a forum.
How should I go about parsing this data? Is there a framework I can use?
Once again, the data I would like to use is the table (forum) at this website - http://www.roblox.com/Forum/Default.aspx.
Thanks!
Upvotes: 1
Views: 1226
Reputation:
I really recommend Hpple. It's a really good XML/HTML parser.
To use it, you'll need to add ${SDKROOT}/usr/include/libxml2
in "Header Search Path", in your project option and add -lxml2
to "Other Linker Flag".
Then, drag it to your code: TFHpple.h
, TFHpple.m
, TFHppleElement.h
, TFHppleElement.m
, XPathQuery.h
, XPathQuery.m
.
In the code add:
NSData *htmlData = [[NSString stringWithContentsOfURL:[NSURL URLWithString: @"http://www.roblox.com/Forum/Default.aspx"]] dataUsingEncoding:NSUTF8StringEncoding];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSArray *elements = [xpathParser searchWithXPathQuery:@"//a[@class='forumTitle']"];
for(int i = 0; i < [elements count]; i++) {
TFHppleElement *element = [elements objectAtIndex:i];
NSString *string = [element content];
NSLog(@"%@", string);
}
[xpathParser release];
[htmlData release];
We made a loop in every a
with class = forumTitle
with TFHppleElement
and NSLog
it.
To understand more, take a look at XPath Syntax. Also check a tutorial.
Now you ask: Why use Hpple? It's faster, more easy to use and with awesome tutorials.
Hope it help!
Upvotes: 1