我遇到了一个奇怪的问题。我正在制作一个应用程序,它应该在iOS 4&5.当我在iOS 5中解析XML时,它做得很好,我得到了我的输出,但当我试图在iOS 4中进行同样的操作时,我得到的错误是"NSXMLParserErrorDomain error 31"。在中
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError;
我的XML是这样的。。我需要解析Item标记中的title标记。。。
<item> <title> <![CDATA[ Cynical approach to tar my reputation: Army Chief on leaked letter ]]> </title> <link> <![CDATA[ ]]> </link> <guid isPermaLink="false"> <![CDATA[ ]]> </guid> </item> <item> <title> <![CDATA[ Major fire in scrap godown in suburban Mumbai ]]> </title> <link> <![CDATA[ ]]> </link> <guid isPermaLink="false"> <![CDATA[ ]]> </guid> </item>
我的解析代码id this:-
-(void)parseBN{
NSString *URL=@"My XML's URL";
// convert the path to a proper NSURL
NSURL *xmlURL = [NSURL URLWithString:URL];
CTNetworking *request= [CTNetworking requestWithUrl:xmlURL];
[request setDelegate:self];
[request setDidFinishSelector:@selector(startParse:)];
[request startRequest];
}
-(void)startParse:(CTNetworking*)response{
if (response.responseStatusCode == 200 && rssParser == nil) {
[stories removeAllObjects];
NSString *myStr = [[NSString alloc] initWithData:[response responseData] encoding:NSWindowsCP1251StringEncoding];
myStr = [myStr stringByReplacingOccurrencesOfString:@"encoding="windows-1251"" withString:@""];
NSData* aData = [myStr dataUsingEncoding:NSASCIIStringEncoding];
// NSXMLParser *parser = [[NSXMLParser alloc] initWithData:aData];
rssParser = [[NSXMLParser alloc] initWithData:aData];
// rssParser = [[NSXMLParser alloc] initWithData:[response responseData]];
// 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
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
}
}
- (void)parserDidStartDocument:(NSXMLParser *)parser{
}
- (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];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
It is not coming here.. so didn't paste the Inside code
}
我在谷歌上搜索了这个错误,每个人都说这个错误是因为未知编码。。所以我尝试使用"NSWindowsCP1251StringEncoding"代替默认编码。如有任何帮助,我们将不胜感激。
终于让它工作了。。。
NSString *myStr = [[[NSString alloc] initWithData:[response responseData] encoding:NSWindowsCP1251StringEncoding] autorelease];
myStr = [myStr stringByReplacingOccurrencesOfString:@"encoding="windows-1251"" withString:@""];
myStr = [myStr stringByReplacingOccurrencesOfString:@"US-ASCII" withString:@"UTF-8"];
NSData* aData = [myStr dataUsingEncoding:NSASCIIStringEncoding];
rssParser = [[NSXMLParser alloc] initWithData:aData];
[rssParser setDelegate:self];
[rssParser setShouldProcessNamespaces:NO];
[rssParser setShouldReportNamespacePrefixes:NO];
[rssParser setShouldResolveExternalEntities:NO];
[rssParser parse];
通过NSString:)更改了编码类型