2010-04-13 8 views
0

UITableViewCell을 눌러 UIWebView<link>을로드하거나 해당 링크에서 Safari를 열면 RSS 리더가 제대로 작동합니다.코코아 터치에서 HTML 문자열을 처리하는 방법

하지만 정말 <body> 태그가 대신 각 <item> 당 RSS 피드에서 사파리

에 전체 사이트 또는 점프를 보여주는 응용 프로그램에 주제 컨텐츠 (그리고이 포함 된 <Description>을로드하는 방법을 배우고 싶어요 그래서

alt text http://www.balexandre.com/temp/2010-04-13_0953.png

, 대신 나는 <body>을 할당있어 <link>을 잡는 : 쇼 아래 이미지처럼, 주제의 내용을 포함하는) 동일하지만 인코딩. 문제는 난 단지 더 첫번째 <br> 아무것도 할 때까지 콘텐츠를이 예를 들어

:-(제대로 작동하지 않는다는 것입니다. 나는 C#에서 사용 하듯이 나는 NSString을 사용하고

  • ,해야 나는 그런 데이터를 사용하는 더 나은 객체가, 다른 물건을 사용?
  • 내가 (이미 스크롤을 가지고 같은)이 정보를로드 할 UITextView를 사용해야하거나 내가 UIWebView 대신?

를 사용한다 Th 너를 괴롭히지.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
// Navigation logic 

int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; 

NSString * storyContent = [[stories objectAtIndex: storyIndex] objectForKey: @"body"]; 

// load web view 
[self showWebPage:storyContent]; // <-- Here (storyContent variable) I only have the content until the first <br> :-(
} 

답변

0

Apple의 SeismicXML 샘플 프로젝트를 살펴보십시오. NSXMLParser을 사용하는 방법에 대한 일반적인 아이디어를 제공하며 사용자가 여전히 UI와 상호 작용할 수 있도록 자체 스레드의 XML 데이터를 구문 분석하기 위해 작성됩니다. 중첩 된 태그 (예 : <body><item>)를 찾으려면 현재 <item> 태그 내에 있는지 여부를 알리는 플래그가 필요합니다. 그렇지 않으면 모든 <body> 태그를 구문 분석합니다.

두 번째 질문을 처리하기 위해이 정보를 사용자에게 제공하려는 방식에 따라 다릅니다. 텍스트 스타일을 원하면 UIWebView를 사용해야합니다. 그렇지 않으면 필요로하는 것을 제거하고 UITableView 또는 Interface Builder에서 만든 사용자 정의보기를 통해 확장 할 수 있습니다.

편집 : 마지막으로 댓글을 본 후 (예 :insideBody) 및 foundCharacters:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ 
    //NSLog(@"found characters: %@", string); 
    // save the characters for the current item... 
    if(insideBody == YES) { 
     [currentBody appendString:string]; 
    } else if ([currentElement isEqualToString:@"title"]) { 
     [currentTitle appendString:string]; 
    } else if ([currentElement isEqualToString:@"link"]) { 
     [currentLink appendString:string]; 
    } else if ([currentElement isEqualToString:@"summary"]) { 
     [currentSummary appendString:string]; 
    } else if ([currentElement isEqualToString:@"pubDate"]) { 
     [currentDate appendString:string]; 
    } else if ([currentElement isEqualToString:@"dc:creator"]) { 
     [currentCreator appendString:string]; 
    } 
} 

의 내부를 확인 당신은 아마 단지 당신에게 태그 아니라 태그 자체의 내용을 줄 것이다 foundCharacters:에서 얻는 데이터로, didStartElement:didEndElement:에서 동일한 작업을 수행해야합니다.

+0

이상한 단지까지 모든 것을 포함하지 않는, 이것은 단지 – balexandre

+0

가있는''태그입니다 NSMutableString을 사용하여 ''태그 안에 문자열을 생성 하시겠습니까? 메모리가 작동하면 태그가 닫힐 때까지 계속 진행해야합니다 (다른 곳에서 파싱중인 태그를 발견하지 못하면). – mgriepentrog

+0

내 코드를 추가 ... NSMutableString 사용할 : - / – balexandre

0

사용 NSXMLParser<body></body> 태그를 감지하고, 단지 그들 사이의 모든 것을 잡아 :


내가 가진 내 웹 브라우저보기에 전달할

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{    
    //NSLog(@"found this element: %@", elementName); 
    currentElement = [elementName copy]; 
    if ([elementName isEqualToString:@"item"]) { 
     // clear out our story item caches... 
     item = [[NSMutableDictionary alloc] init]; 
     currentTitle = [[NSMutableString alloc] init]; 
     currentDate = [[NSMutableString alloc] init]; 
     currentSummary = [[NSMutableString alloc] init]; 
     currentLink = [[NSMutableString alloc] init]; 
     currentBody = [NSMutableString new]; // added by me 
     currentCreator = [NSMutableString new]; // added by me 
    }  
} 
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{  
    //NSLog(@"ended element: %@", elementName); 
    if ([elementName isEqualToString:@"item"]) { 
     // save values to an item, then store that item into the array... 
     [item setObject:currentTitle forKey:@"title"]; 
     [item setObject:currentLink forKey:@"link"]; 
     [item setObject:currentSummary forKey:@"summary"]; 
     [item setObject:currentDate forKey:@"date"]; 
     [item setObject:currentBody forKey:@"body"]; // <---- 
     [item setObject:currentCreator forKey:@"dc:creator"]; 

     [stories addObject:[item copy]]; 
     NSLog(@"adding story: %@", currentTitle); 
    } 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ 
    //NSLog(@"found characters: %@", string); 
    // save the characters for the current item... 
    if ([currentElement isEqualToString:@"title"]) { 
     [currentTitle appendString:string]; 
    } else if ([currentElement isEqualToString:@"link"]) { 
     [currentLink appendString:string]; 
    } else if ([currentElement isEqualToString:@"summary"]) { 
     [currentSummary appendString:string]; 
    } else if ([currentElement isEqualToString:@"pubDate"]) { 
     [currentDate appendString:string]; 
    } else if ([currentElement isEqualToString:@"body"]) { 
     [currentBody appendString:string]; // <---- 
    } else if ([currentElement isEqualToString:@"dc:creator"]) { 
     [currentCreator appendString:string]; 
    } 
} 

추가 문자열로.

이제 코드를 볼 수 있으므로 버그를 볼 수 있습니다. <br /> 태그를 발견하면 currentElement@"br"으로 변경합니다. 즉, 문자 추가를 더 이상 중지하지 않습니다. currentBody.

+0

내가 이미하고 있어요 - 문자열이 처음
마녀가 이미하여 RSS를 얻기 위해 NSXMLParser를 사용하고 – balexandre

관련 문제