2012-03-30 62 views
0

我得到一個奇怪的問題。我正在做一個應用程序,應該在兩個iOS上運行4 & 5.當我解析我的XML在iOS 5它做得很好,我得到我的輸出,但是當我試圖在做同樣的iOS 4,我收到此錯誤「NSXMLParserErrorDomain error 31」。在NSXMLParserErrorDomain解析XML時

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError; 

我的XML是這樣的..我需要解析產品標籤內的標題標籤...

<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 *[email protected]"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

最後我得到它的工作...

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更改編碼類型:)