我遇到了一个奇怪的问题。我正在制作一个应该在iOS 4和iOS上运行的应用程序5.当我在 iOS 5中解析我的XML它做得很好而且我正在获取输出,但是当我尝试在 iOS 4中执行相同操作时,我收到此错误“ NSXMLParserErrorDomain错误31 “。在
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError;
我的XML就像这样......我需要在Item标签中解析标题标签......
<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>
我的解析代码是这样的: -
-(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 ”而不是默认编码。 任何帮助都会得到赞赏。
答案 0 :(得分:0)
最后我开始工作......
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更改了编码类型:)