2011-05-03 2 views
4

나는 iPad platfrom 용 학교 신문 앱에서 작업 해왔다. NSXMLParser를 사용하여 각 기사의 제목, 간단한 설명 및 링크를 얻습니다. 각 파싱 된 링크에서 HTML 항목을 가져 오기 위해 Hpple 파서를 사용하기로 결정했습니다. 나는 파싱하고 RSS 항목을 올바르게 저장하고 있다고 생각하지만 for 루프를 사용하여 각 구문 분석 된 링크에서 HTML 항목을 구문 분석하려고하면 RSS 항목에 대해 빈 배열이 있다고 알립니다. 그러나 RSS 항목 소유자의 콘텐츠를 콘솔에 표시 할 수 있습니다. 그래서, 그것은 비어 있지 않습니다. 내 코드의 일부분을 콘솔에서 표시 할 것입니다. 제발 도와주세요. 이 프로젝트의 기한은 곧 있습니다. 미리 감사드립니다. 여기Hpple 파서와 NSXMLParser를 사용하여 내부 HTML을 반복적으로 파싱

내가 내 RSS 파서 (articleParser)로드 시작하는 방법입니다

- (void)loadData { 
    [self loadInitData]; 

    //[self loadDataWithLink]; 

} 

- (void)loadInitData { 
    if (sections == nil) { 
     [activityIndicator startAnimating]; 

     NSLog(@"STARTING ARTICLE PARSER FROM MAIN URL!!!"); 

     Parser *articleParser = [[Parser alloc] init]; 
     [articleParser parseRssFeed:@"http://theaggie.org/rss/headlines.xml" withDelegate:self]; 
     [articleParser release]; 
    } else { 

    } 

} 

을 그리고 아래 내가 "섹션"이라고 NSMutable 배열에서 받았습니다 조 항목을 저장하는 방법입니다. 그런 다음 구문 분석 된 각 링크를 반복하는 데 for 루프를 사용했습니다. 다음은

- (void)receivedArticleItems:(Article *)theArticle { 
    if (sections == nil) { 
     sections = [[NSMutableArray alloc] init]; 
    } 
    [sections addObject:theArticle]; 

    NSLog(@"We recieved the article!"); 
    NSLog(@"Article: %@", theArticle); 
    NSLog(@"What is in sections: %@", sections); 

for (int i = 1; i < 5; i++) { 
     NSLog(@"articleItems: %@",[sections objectAtIndex:0]); 
     NSLog(@"articleItems at index 0: %@",[[[sections objectAtIndex:0] articleItems] objectAtIndex:0]); 

     [self loadDataWithLink:[[[[sections objectAtIndex:0] articleItems] objectAtIndex:0] objectForKey:@"link"]]; 
    } 
    [activityIndicator stopAnimating]; 
} 

내가 각 구문 분석 링크에서 HTML 항목을 얻을 수 TFFHpple 파서를 사용하는 방법입니다

- (void)loadDataWithLink:(NSString *)urlString{ 

NSData *htmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]; 

// Create parser 
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData]; 

//Get all the cells main body 
htmlElements = [xpathParser search:@"//div[@id='main']/div[@id='mainCol1']/div[@id='main-body']"]; 

// Access the first cell 
TFHppleElement *htmlElement = [htmlElements objectAtIndex:0]; 

// NSString *title = [htmlElement content]; 

NSLog(@"What is in element: %@", htmlElement); 

[xpathParser release]; 
//[htmlData release]; 
} 

을 그리고 이것은 내가 콘솔에 얻고 무엇을 :

2011-05-02 22:58:35.355 TheCalAggie[2443:207] Parsing started for article! 
2011-05-02 22:58:35.356 TheCalAggie[2443:207] Adding story title: Students say, 'No time for books' 
2011-05-02 22:58:35.356 TheCalAggie[2443:207] From the link: http://theaggie.org/article/2011/05/03/students-say-no-time-for-books 
2011-05-02 22:58:35.357 TheCalAggie[2443:207] Summary: The last book managerial economics major Kiyan Parsa read for fun was The Lord of the Rings. That was in high school. 
2011-05-02 22:58:35.358 TheCalAggie[2443:207] Published on: Tue, 03 May 2011 00:00:00 -0700 
2011-05-02 22:58:35.359 TheCalAggie[2443:207] Parsing started for article! 
2011-05-02 22:58:35.360 TheCalAggie[2443:207] Adding story title: UC Davis craft center one of largest college crafting centers 
2011-05-02 22:58:35.360 TheCalAggie[2443:207] From the link: http://theaggie.org/article/2011/05/02/uc-davis-craft-center-one-of-largest-college-crafting-centers 
2011-05-02 22:58:35.361 TheCalAggie[2443:207] Summary: Hidden away in the South Silo, the UC Davis Craft Center offers 10 craft studios and more than a hundred classes for students looking to learn or perfect their crafting skills. 
2011-05-02 22:58:35.362 TheCalAggie[2443:207] Published on: Mon, 02 May 2011 00:00:00 -0700 
2011-05-02 22:58:35.362 TheCalAggie[2443:207] We recieved the article! 
2011-05-02 22:58:35.363 TheCalAggie[2443:207] Article: *nil description* 
2011-05-02 22:58:35.364 TheCalAggie[2443:207] What is in sections: (
    (null) 
) 
2011-05-02 22:58:35.374 TheCalAggie[2443:207] articleItems: *nil description* 
2011-05-02 22:58:35.375 TheCalAggie[2443:207] articleItems at index 0: { 
    link = "http://theaggie.org/article/2011/05/03/peaceful-rally-held-on-campus-after-killing-of-bin-laden\n"; 
    pubDate = "Tue, 03 May 2011 00:00:00 -0700"; 
    summary = "The announcement of Osama bin Laden's death sent a wave of patriotism across the nation and UC Davis. Bin Laden was the leader of al-Qaeda - the organization allegedly behind the Sept. 11, 2001 attacks that killed over 3,000 Americans.\n"; 
    title = "Peaceful rally held on campus after killing of bin Laden \n"; 
} 
2011-05-02 22:59:35.376 TheCalAggie[2443:207] Unable to parse. 
2011-05-02 22:59:35.379 TheCalAggie[2443:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSMutableArray objectAtIndex:]: index 0 beyond bounds for empty array' 
*** Call stack at first throw: 

어떤 도움 대단히 감사하겠습니다. 다시 한번 감사드립니다.

답변

3

2011-05-02 22 : 59 : 35.376 TheCalAggie [2443 : 207] 구문 분석 할 수 없습니다.

파서가 HTML 구문 분석에 어려움을 겪고 있습니다. 파서는 HTML 구문 분석에 완벽하지 않습니다. 잠재적으로 깨진/유효하지 않은 HTML 문서에 대해 XPath를 실행하는 구문 분석을위한 복잡한 작업입니다.

W3C 유효성 검사기 here을 통해 구문 분석하려는 링크를 전달하면 오류가 발생합니다. 그래서 그것의 완전히 유효하지 않은 HTML. 파서가 파싱하기에 너무 부러진다면 디버깅하고 알아 내야 할 것이 있습니다. 실제로이 내용의 맨 아래로 가려면 더 자세한 정보를 찾으려고 사용하는 TFHpple 파서에 중단 점을 설정해야합니다.

+0

감사합니다. Damien !! 나는 HTML 소스 코드를 파고 마침내 내가 필요한 것을 파싱 할 수 있었다. 이제 MVC에 다른 문제가 있습니다. 나는 또 다른 질문을 올렸다. 당신도 저를 도울 수 있습니까? 다음은 해당 질문에 대한 링크입니다. http://stackoverflow.com/questions/6132894/passing-uitextview-values-to-modalviewcontroller-from-parent-view-controller – SerPiero

0

Damien이 옳습니다. 먼저 코드를 작동 시키려면 html을 수정해야합니다. 구문 분석하는 데이터는 매번 다릅니다. 이는 HTML이 버그가 있음을 증명합니다. 따라서 코드가 어떤 경우에는 작동 할 수 있습니다. 몇 번 실행 해보십시오. 때때로 작동하는 것을 볼 수 있습니다.

+0

예. 마침내 내가 필요한 것을 파싱 할 수 있었다. HTML 소스 코드를 파헤쳐 야했습니다. 응답 주셔서 감사합니다. 나는 당신이 나를 도울 수 있다면 또 다른 질문을 가지고있다. 링크는 다음과 같습니다. http://stackoverflow.com/questions/6132894/passing-uitextview-values-to-modalviewcontroller-from-parent-view-controller – SerPiero

관련 문제