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