xml parsing problem in iPhone

2019-08-02 03:16发布

I have this xml feed in Chinese, which NSXLParsers is unable to parse. It gives error 31 while parsing.

error 31 occur due to Document encoding is unknown.

I tried UTF-8 and ascii encodings to convert string rendered via

[NSString stringwithContentsOfURL:@"http://news.baidu.com/n?cmd=4&class=finannews&tn=rss"]

to corresponding format.

Can any body shed some light on how to parse XML feeds written in another languages.

Thanx in advance

3条回答
ゆ 、 Hurt°
2楼-- · 2019-08-02 03:59

Try to debugg using

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

    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

It will help to find whether the problem is with the xml or the encoding method

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-08-02 04:01

The answer is the first line of the feed

<?xml version="1.0" encoding="gb2312"?>

Then read about gb2312

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-08-02 04:11

Since the file is encoded in GB2312, you should provide an encoding with

[NSString stringWithContentsOfURL:@"http://..."
                         encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_2312_80)
                            error:NULL];
查看更多
登录 后发表回答