0

RSS パーサーのコードがありますが、CDATA を読み取ることができません。次のコードで何を変更する必要があるか、誰かに教えてもらえますか:


#import "BlogRssParser.h"
#import "BlogRss.h"

@implementation BlogRssParser

@synthesize currentItem = _currentItem;
@synthesize currentItemValue = _currentItemValue;
@synthesize rssItems = _rssItems;
@synthesize delegate = _delegate;
@synthesize retrieverQueue = _retrieverQueue;

- (id)init{
    if(![super init]){
        return nil;
    }
    _rssItems = [[NSMutableArray alloc]init];
    return self;
}

- (NSOperationQueue *)retrieverQueue {
    if(nil == _retrieverQueue) {
    _retrieverQueue = [[NSOperationQueue alloc] init];
    _retrieverQueue.maxConcurrentOperationCount = 1;
}
return _retrieverQueue;
}

- (void)startProcess{
SEL method = @selector(fetchAndParseRss);
[[self rssItems] removeAllObjects];
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self 
                                                                 selector:method 
                                                                   object:nil];
[self.retrieverQueue addOperation:op];
[op release];
}

-(BOOL)fetchAndParseRss{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

//To suppress the leak in NSXMLParser
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];

NSURL *url = [NSURL URLWithString:@"http://www.myfeed.com/feed"];
BOOL success = NO;
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:YES];
[parser setShouldReportNamespacePrefixes:YES];
[parser setShouldResolveExternalEntities:NO];
success = [parser parse];
[parser release];
[pool drain];
return success;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{
if(nil != qualifiedName){
    elementName = qualifiedName;
}
if ([elementName isEqualToString:@"item"]) {
    self.currentItem = [[[BlogRss alloc]init]autorelease];
} else if([elementName isEqualToString:@"title"] || 
          [elementName isEqualToString:@"link"] ||
          [elementName isEqualToString:@"pubDate"] ||
          [elementName isEqualToString:@"guid"] ||
          [elementName isEqualToString:@"description"]) {
    self.currentItemValue = [NSMutableString string];
} else {
    self.currentItemValue = nil;
}   
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if(nil != qName){
    elementName = qName;
}
if([elementName isEqualToString:@"title"]){
    self.currentItem.title = self.currentItemValue;
}else if([elementName isEqualToString:@"link"]){
    self.currentItem.linkUrl = self.currentItemValue;
}else if([elementName isEqualToString:@"pubDate"]){
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
    self.currentItem.pubDate = [formatter dateFromString:self.currentItemValue];
    [formatter release];
}else if([elementName isEqualToString:@"guid"]){
    self.currentItem.guidUrl = self.currentItemValue;
}else if([elementName isEqualToString:@"description"]){
    self.currentItem.description = self.currentItemValue;
}else if([elementName isEqualToString:@"item"]){
    [[self rssItems] addObject:self.currentItem];
}
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(nil != self.currentItemValue){

    [self.currentItemValue appendString:string];
}

}

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock{

// I think something here!?!?

}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{
if(parseError.code != NSXMLParserDelegateAbortedParseError) {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [(id)[self delegate] performSelectorOnMainThread:@selector(processHasErrors)
                                          withObject:nil
                                       waitUntilDone:NO];
}
}



- (void)parserDidEndDocument:(NSXMLParser *)parser {
[(id)[self delegate] performSelectorOnMainThread:@selector(processCompleted)
                                      withObject:nil
                                   waitUntilDone:NO];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}


-(void)dealloc{
self.currentItem = nil;
self.currentItemValue = nil;
self.delegate = nil;

[_rssItems release];
[super dealloc];
}

@end

このコードは、説明のみが CDATA を使用したフィードに使用されます。他のアイテムには CDATA がなく、アプリによって表示されました。

助けてくれてありがとう

4

1 に答える 1

0

だから私は最初のステップを見つけました。私は自分のコードでこれを変更しました:

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock{

 self.currentItem.description = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];

}

これで、HTMLタグ付きのBURの説明が表示されますか?誰かが知っていますか、どうすればHTMLタグを無視できますか?

どうもありがとうマティアス

于 2012-07-28T16:29:44.127 に答える