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

发布于 2024-12-07 13:09:28 字数 590 浏览 0 评论 0原文

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

Windows-1256 的 rss 文件

已解决 解决方案是

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];

i want to parse rss file in Windows-1256" encoding but it is not being read by parser
i done alot of parsing in UTF8 encoding but only this don't work why?

rss file with Windows-1256

Solved
solution is

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天气好吗我好吗 2024-12-14 13:09:29

如果您在 NSXMLParser 委托中实现 parseErrorOccurred: 方法,它将为您提供错误的确切原因。

像这样的东西:

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

If you implement the parseErrorOccurred: method in your NSXMLParser delegate, it will give you the exact reason for the errors.

Something like:

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSLog(@"NSXMLParser ERROR: %@ - %@", , [parseError localizedDescription], [parseError localizedFailureReason]);
}
网白 2024-12-14 13:09:28

谢谢 Mohamed 的回答,我已经研究了 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];

}

Thank you Mohamed for the answer I kept working on it for 10 days and we found no answers at all. This is my code:

-(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];

}

旧梦荧光笔 2024-12-14 13:09:28

你也可以尝试这个:

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

Also you can try this:

int length = str.length >100 ? 100:str.length;
NSString*mystr= [str stringByReplacingOccurrencesOfString:@"encoding=\".*?\"" 
                    withString:@""
                    options:NSRegularExpressionSearch 
                    range:NSMakeRange(0, length)];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文