NSXMLParser解析使用Windows-1256编码的xml文件

时间:2011-10-03 16:15:08

标签: iphone xml ios encoding nsxmlparser

我想在Windows-1256“编码中解析rss文件,但解析器没有读取它 我在UTF8编码中做了很多解析,但只有这个不起作用?

rss file with Windows-1256

解决 解决方案是

NSString *myStr = [[NSString alloc] initWithData:myData encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsArabic) ];
myStr = [myStr stringByReplacingOccurrencesOfString:@"encoding=\"windows-1251\"" withString:@""];        
NSData* aData = [myStr dataUsingEncoding:NSUTF8StringEncoding];    
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:aData];

3 个答案:

答案 0 :(得分:4)

谢谢穆罕默德的答复,我一直在努力工作10天,我们根本找不到任何答案。这是我的代码:

-(void)parseXMLFileAtURL:(NSString *)URL {

NSURL *xmlURL = [NSURL URLWithString:URL];
NSData * dataXml = [[NSData alloc] initWithContentsOfURL:xmlURL];
NSString *myStr = [[NSString alloc] initWithData:dataXml encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsArabic)];

myStr = [myStr stringByReplacingOccurrencesOfString:@"encoding=\"windows-1256\"" withString:@""];
NSData *aData = [myStr dataUsingEncoding:NSUTF8StringEncoding];

NSXMLParser *rssParser = [[NSXMLParser alloc] initWithData:aData];
[dataXml release];
[rssParser setDelegate:self];
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
[rssParser setDelegate:nil];
[rssParser release];

}

答案 1 :(得分:1)

你也可以试试这个:

int length = str.length >100 ? 100:str.length;
NSString*mystr= [str stringByReplacingOccurrencesOfString:@"encoding=\".*?\"" 
                    withString:@""
                    options:NSRegularExpressionSearch 
                    range:NSMakeRange(0, length)];

答案 2 :(得分:0)

如果您在parseErrorOccurred:代表中实施NSXMLParser方法,它会告诉您错误的确切原因。

类似的东西:

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSLog(@"NSXMLParser ERROR: %@ - %@", , [parseError localizedDescription], [parseError localizedFailureReason]);
}