2010-07-28 6 views
3

i essayer d'analyser dans mon iPhone SDK 4iPhone NSXMLParser (erreur 9)

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

il y a un peu Umlaute allemand

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

Comme je l'ai lu dans un autre forum aussi longtemps qu'ils sont enveloppés dans CDATA ça devrait aller.

Mais au moment où l'analyseur a trouvé l'élément « description » il rompt avec:

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 

Les flux anglais fonctionne très bien! Donc c'est quelque chose avec cet umlaute, mais que puis-je faire?

salue chris

JUSTE POUR COMPRENDRE .. ICI tout mon ANALYSEUR

- (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]); 
} 
+0

ne vous obtenez toujours l'erreur si vous analysez simplement la chaîne __ @ » __? Sinon, il est autre chose alors le – deanWombourne

+0

? Trémas. Où dois-je changer quoi? :) Je lis aussi dans d'autres forums que cela arrive surtout quand il y a umlaute, mais pas de vraies solutions, juste pour comprendre j'ai placé tout mon analyseur dans ma question –

Répondre

3

regarder Peut-être en -stringWithContentsOfURL:usedEncoding:error: pour télécharger le 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]; 
+0

thx .. j'ai essayé ... je suppose que c'est dans la bonne direction, mais toujours CODE D'ERREUR 5 .. seulement avec cette URL, il semble que ce gars l'a résolu pour un autre Pars Euh, juste je ne sais pas comment mettre en œuvre dans mon code: http://petersteinberger.com/2010/06/use-tturlxmlresponse-avec-serveur-que-vous-voulez-vous-l'encre-rangere/ –

0

De l'documentation:

NSXMLParserInvalidCharacterError = 9 

Peut-être que le document n'est pas vraiment encodées en UTF-8?

+0

Le problème est que le document est encodé en utf-8 mais yahoo réponse du serveur ISO-8859-1 vous pouvez voir par: http://beta.feedvalidator.org –

0

Maintenant, je l'ai résolu différent. A écrit mon propre analyseur XML juste simple pour mes besoins et avec la routine suivante, j'ai trouvé dans un forum, je code la chaîne xml. J'ai également marqué la réponse ci-dessus comme «acceptée» car elle m'a conduit dans la bonne direction.

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)s { 
    [resultString appendString:s]; 
} 

- (NSString*)convertEntiesInString:(NSString*)s { 
    resultString = [[NSMutableString alloc] init]; 

    if(s == nil) { 
     //NSLog(@"ERROR : Parameter string is nil"); 
    } 
    NSString* xmlStr = [NSString stringWithFormat:@"<d>%@</d>", s]; 
    NSData *data = [xmlStr dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 
    NSXMLParser* xmlParse = [[NSXMLParser alloc] initWithData:data]; 
    [xmlParse setDelegate:self]; 
    [xmlParse parse]; 
    NSString* returnStr = [[NSString alloc] initWithFormat:@"%@",resultString]; 

    return returnStr; 
} 


objectsResultStr = [self convertEntiesInString:orgString]];