2013-03-03 5 views
2

HTML을 구문 분석하기 위해 Hpple을 사용하고 있으며 사실 XML이라고 인식하지 못하는 것 같습니다 (XCode 디버거는이 변수를 isXML = (BOOL) NO으로 표시하고 데이터를 수집하지 않습니다) . 이 문제를 어떻게 해결할 수 있습니까?Hpple은 HTML을 구문 분석 할 수 없습니다

이것은 내 코드입니다 (다른 버그 일 수도 있습니다). 구문 분석 방법/기능 [ListParser parse:@"http://www.fanfiction.net/book/Harry-Potter/" at:@"//div[@=\"class\"]"]; 먼저 호출됩니다

@interface ListParser() //private 
+ (NSArray*) getNodeListAt: (NSURL*) page inside: (NSString*) page; 
+ (NSDictionary*) getNodeData: (TFHppleElement*) node; 
+ (void) addMiniListData: (NSString*) list to: (NSMutableDictionary*) dict; 
@end 


@implementation ListParser 

+ (NSArray*) getNodeListAt: (NSURL*) page inside: (NSString*) path { // "//div[@class"z-list"]" 
    NSData *data = [NSData dataWithContentsOfURL: page]; 
    TFHpple *listparser = [TFHpple hppleWithHTMLData:data]; //WHERE CODE SEEMS TO STOP TO WORK 
    NSArray *done = [listparser searchWithXPathQuery: path]; 
    return done; 
} 

+ (void) addMiniListData: (NSString*) list to: (NSMutableDictionary*) dict{ 
    NSArray *parts = [list componentsSeparatedByString:@" - "]; 

    for(NSString* p in parts){ 
     NSArray* two = [p componentsSeparatedByString:@": "]; 
     [dict setObject:[two objectAtIndex:1] forKey:[two objectAtIndex:0]]; 
    } 
} 

+ (NSDictionary*) getNodeData: (TFHppleElement*) node{ 
    NSMutableDictionary* data = [NSMutableDictionary dictionary]; 
    [data setObject:[[[node firstChild] firstChild] objectForKey:@"href"] forKey:@"Image"]; 
    [data setObject:[[node firstChild] text] forKey:@"Title"]; 
    [data setObject:[[[[node firstChild] children] objectAtIndex:2] text] forKey:@"By"]; 
    [data setObject:[[[[node firstChild] childrenWithClassName:@"z-indent"] objectAtIndex:0] text] forKey:@"Summery"]; 
    [self addMiniListData:[[[[[[node firstChild] childrenWithClassName:@"z-indent"] objectAtIndex:0] childrenWithClassName:@"z-padtop2"] objectAtIndex:0] text] to: data]; 

    return data; 
} 

+(NSArray*) parse: (NSString*) address at: (NSString*) path{ 
    NSURL *url = [[NSURL alloc] initWithString:address]; 
    NSArray* list = [self getNodeListAt:url inside:path]; 
    NSMutableArray *data = [[NSMutableArray alloc] init]; 
    for (TFHppleElement* e in list) { 
     [data addObject:[self getNodeData:e]]; 
    } 
    return [[NSArray alloc] initWithArray: data]; 
} 

@end 

여기 튜토리얼 나는 다음과 같은 한에 대한 링크 : 당신이 TFHpple와 XML을 구문 분석해야하는 경우 http://www.raywenderlich.com/14172/how-to-parse-html-on-ios

+0

HTML은 XML이 아닙니다. – nneonneo

+0

@nneonneo 나는 알고있다. 그러나 그것은 양쪽 모두를 위해 일한다고 생각된다. 적어도 웹 사이트와 여러 자습서에 따르면 HTML로 XML을 인식한다고합니다. –

답변

0

, 당신이 그것을 말해야 당신 ' 그렇게하고있어. 전화가 +hppleWithHTMLData:입니다. 이 메서드의 구현을 읽으면 isXMLNO으로 설정한다는 것을 알 수 있습니다. 대신 hppleWithXMLData: 방법을 사용하십시오.

관련 문제