본문 바로가기

C/Objective C/ios

아이폰 HtmlParsing 예제 따라하기

- 외부 코드를 이용해서,  Html 파싱을 해보도록 하자.

- Html  파싱은 좋은 방법이 아님으로 권장하고 싶지 않다.


- HtmlParsing 예제 따라하기

1. 브라우저를 이용하여 아래의 사이트 방문(아래에 다운받아도 가능하다)

http://blog.objectgraph.com/index.php/2010/02/24/parsing-html-iphone-development/

hpple.zip

화면 아래 부분에 있는 Donwnload the complete project file is available here를 클릭하여 파일

다운로드
2. Xcode 실행
3. Single View Application
4. Project Name : “HtmlParsingTest”
5. 1.에서 다운받은 파일에 있는 hipple 폴더를 Classes 폴더로 드래그 6. PROJECT 아래에 있는 HtmlPasingTest 클릭

6.1. [Building Settings] - [All] - [Levels] 순서대로 클릭
6.2. 우측 검색 창에 “Header Search Paths”입력
6.3. 찾은 항목 오른쪽 부분을 더블클릭 - + 클릭 - “${SDKROOT}/usr/include/libxml2“ 입력 6.4. 우측 검색 창에 “Other Linker Flags”입력
6.3. 찾은 항목 오른쪽 부분을 더블클릭 - + 클릭 - “-lxml2“ 입력

7. ViewController.m 파일 편집

page1image7376

#import "TFHpple.h" #import "TFHppleElement.h" #import "XPathQuery.h“

-(void)viewDidLoad { [super viewDidLoad];

NSString *htmlWillInsert = [NSString stringWithContentsOfURL:
[NSURL URLWithString:@"http://appcenter.ks.ac.kr"]

encoding:NSUTF8StringEncoding error:nil]; NSData *htmlData = [htmlWillInsert dataUsingEncoding:NSUnicodesStringEncoding];

TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData]; //NSArray *elements = [xpathParser search:@"//table[@width='700']//font"]; NSArray *elements = [xpathParser search:@"//a"];

for(int i = 0; i<[elements count]; i++) {

TFHppleElement *element = [elements objectAtIndex:1]; NSString *titleOfAlips = [element content];

NSLog(@"%@", titleOfAlips); }

}

8. 디버깅 

9. 실행 


- ViewController.h 소스코드


#import <UIKit/UIKit.h>

#import "TFHpple.h" 

#import "TFHppleElement.h" 

#import "XPathQuery.h"


@interface ViewController : UIViewController


@end

- ViewController.m 소스코드

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

NSLog(@"connectWebsite");

    

    NSString *htmlURL = [NSString stringWithContentsOfURL:

                         [NSURL URLWithString:@"http://appcenter.ks.ac.kr"

encoding:NSUTF8StringEncoding error:nil];

//-2147481280 -> NSUTF8StringEncoding

    NSData *htmlData = [htmlURL dataUsingEncoding:NSUnicodeStringEncoding];

    if (htmlData != nil) {

        TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];

        NSArray *parserArray  = [xpathParser search:@"//*"];

        for (int i = 0; i < [parserArray count]; i++) {

            TFHppleElement *element = [parserArray objectAtIndex:i];

            NSLog(@"%@", [element content]);

        }

    }

    else {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"연결 실패"

                                                        message:@"데이터를 가져올 없습니다."

                                                       delegate:self cancelButtonTitle:@"확인" otherButtonTitles:nil];

        [alert show];

    }

    NSLog(@"connectWebsiteEnd");

}


- (void)viewDidUnload

{

    [super viewDidUnload];

    // Release any retained subviews of the main view.

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

} else {

    return YES;

}

}


@end



- 실행화면