iPhone NSXMLParser(错误 9) [英] iPhone NSXMLParser (Error 9)

查看:56
本文介绍了iPhone NSXMLParser(错误 9)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的 iPhone SDK 4 中解析

i try to parse within my iPhone SDK 4

http://de.news.search.yahoo.com/news/rss?p=iphone&ei=UTF-8&fl=0&x=wrt

有一些德国元音

<description><![CDATA[Mehr als die Hälfte der Belegschaft des weltweit größten]]></description>

正如我在另一个论坛上读到的,只要它们被包裹在 CDATA 中就应该没问题.

As I read in another forum as long they are wrapped in CDATA it should be fine.

但是在解析器找到元素描述"的那一刻他打破了:

But in the moment the parser found the element "description" he breaks with:

error parsing XML: Unable to download story feed from web site (Error code 9 ) http://de.news.search.yahoo.com/news/rss?p=iphone&ei=UTF-8&fl=0&x=wrt

英文提要工作正常!?所以它和这个元音变音有关,但我能做什么?

The english feeds works fine !? So its something with this umlaute, but what can i do?

问候克里斯

只是为了理解......这里是我的整个解析器

JUST FOR UNDERSTANDING .. HERE MY WHOLE PARSER

- (void)parseXMLFileAtURL:(NSString *)URL { 
    aktuelleUrl = URL;
    stories = [[NSMutableArray alloc] init];
    NSURL *xmlURL = [NSURL URLWithString:aktuelleUrl];

// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[rssParser setDelegate:self];

// Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];    
[rssParser parse];

}
- (void)parserDidStartDocument:(NSXMLParser *)parser{   
//NSLog(@"found file and started parsing");

}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:@"Unable to download story feed from web site (Error code %i ) %@", [parseError code], aktuelleUrl];
NSLog(@"error parsing XML: %@", errorString);

}



- (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:@"channel"]) {
    channel1item2 = 1;
    // 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];
}

if ([elementName isEqualToString:@"item"]) {
    channel1item2 = 2;
    // 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];
    currentEncoded = [[NSMutableString alloc] init];

    }   
    }
     - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{     
//NSLog(@"ended element: %@ c1i2: %i", elementName, channel1item2);

if (channel1item2 == 1) {
    if (![currentTitle isEqualToString:@""]) { aCurrentTitle = currentTitle;  }
    if (![currentLink isEqualToString:@""])  { aCurrentLink = currentLink; }
    if (![currentSummary isEqualToString:@""])  {aCurrentSummary = currentSummary; }
}
else if ([elementName isEqualToString:@"item"]) {
    [item setObject:currentTitle forKey:@"title"];
    [item setObject:currentLink forKey:@"link"];
    [item setObject:currentSummary forKey:@"summary"];
    [item setObject:currentDate forKey:@"date"];
    [item setObject:currentEncoded forKey:@"content:encoded"];      
    [stories addObject:[item copy]];
}   
}
- (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];
    //NSLog(@"parselink '%@'",string);
} else if ([currentElement isEqualToString:@"description"]) {
    [currentSummary appendString:string];
} else if ([currentElement isEqualToString:@"pubDate"]) {
    [currentDate appendString:string];
} else if ([currentElement isEqualToString:@"content:encoded"]) {
    [currentEncoded appendString:string];
}
else if ([currentElement isEqualToString:@"media:content"]) {
    //NSLog(@"mediacontent %@",string);
}   
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {

// NSLog(@"all done!");
//NSLog(@"stories array has %d items", [stories count]);
}

推荐答案

也许看看 -stringWithContentsOfURL:usedEncoding:error: 下载 XML:

Perhaps look into -stringWithContentsOfURL:usedEncoding:error: to download the XML:

NSError *error = nil;
NSStringEncoding encoding;
NSString *xmlFeedStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://de.news.search.yahoo.com/news/rss?p=iphone&ei=UTF-8&fl=0&x=wrt"] usedEncoding:&encoding error:&error];
NSXMLParser *rssParser = [[NSXMLParser alloc] initWithData:[xmlFeedStr dataUsingEncoding:encoding allowLossyConversion:YES]];
...
[rssParser release];

这篇关于iPhone NSXMLParser(错误 9)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆