Friday, February 25, 2011

parsing HTML file for iPhone apps using xcode, obj-c

Hay,

Recently I made a project where the application need to read the HTML file , parse it and display the data on the view.

Before you start , get the below files from https://github.com/topfunky/hpple


  1. HTFpple.h
  2. HTFpple.m
  3. HTFppleElement.h
  4. HTFppleElement.m
  5. XPathQuery.h
  6. XPathQuery.m

Once you get that, just do the following modifications to your project settings:

1) Goto Target on the left hand side tree. Right click on your project name and hit Get Info option from popup menu.

2) Select All configurations from Configuration combo box.

3) Search for Header Search path and the bloodline with recursive option selected

${SDKROOT}/usr/include/libxml2

4) In the same way, search for Other Linker Flag


add the below line to it:


-lxml2 


Here is the code to parse the HTML file. [ This code works for reading local HTML file attached in your project ]







NSMutableArray *array = [[NSMutableArray alloc]init];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"myHTMLfileNameWithoutExtension" ofType:@"html"];
NSData *htmlData = [[NSData alloc] initWithContentsOfFile:filePath];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSArray *chapterName  = [xpathParser search:@"//html//h1"]; // get the page title
self.chapterIndexData = chapterName;
[xpathParser release];
[htmlData release];
[array release];


Above code reads all the H1 tags in the HTML page and put them in 

chapterIndexData
  which is of type NSArray *.



Please let me know if you have any doubts regarding this.




2 comments:

  1. It’s never too late to develop your data and your contents really excite me a lot.

    iPhone developer in Pakistan

    ReplyDelete
  2. What does it mean when your data doesn't display in IOS 7.1, but is downloaded and in the array NsArray you create?

    ReplyDelete