NSXMLParser rss release NSXMLParserInvalidCharacterError

NSXMLParserInvalidCharacterError # 9

This is the error I get when I click on a weird character (for example, quotation marks are copied and pasted from a word into a web form that fall into the feed). The feed I use does not give an encoding, and I have no hope of forcing them to change this. This is all I get in the header:

<? xml version = "1.0"? > <rss version = "2.0">

What can I do for illegal characters when analyzing feeds? Am I reviewing the data before parsing? Is there something I am missing in the API? Has anyone dealt with this problem?

+3
source share
3 answers
NSString *dataString = [[[NSString alloc] initWithData:webData encoding:NSASCIIStringEncoding] autorelease];

NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];

...

+7

NSString -initWithData:encoding: nil, , , , . , , XML, , , .

:

// try the most likely encoding
NSString xmlString = [[NSString alloc] initWithData:xmlData 
                                           encoding:NSUTF8StringEncoding];

if (xmlString == nil) {
  // try the next likely encoding
  xmlString = [[NSString alloc] initWithData:xmlData 
                                     encoding:NSWindowsCP1252StringEncoding];
}

if (xmlString == nil) {
  // etc...
}

, :

1.) , Content-Type HTTP ( )

2.) , ,

3.) ; '<' nul/zero, UTF-16 (, , , UTF-32)

4.) , <?xml ... ?>, encoding='something' ; .

5.) . Windows Latin-1, Mac Roman ISO Latin-1, .

6.) , 127 ( "?" ASCII) ASCII.

NSString, . NSString, encoding <?xml ... ?> ( 4). , NSString NSData, ; , UTF-8.

, CFStringConvertIANACharSetNameToEncoding() CFStringConvertEncodingToNSStringEncoding() NSStringEncoding, , Content-Type <?xml ... ?>.

+1

You can also remove this encoding line from xml as follows:

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

Source: https://habr.com/ru/post/1713980/


All Articles