需要幫助將 NSXMLParser 與 ASIHTTPRequest 一起使用 (need help using NSXMLParser with ASIHTTPRequest)


問題描述

需要幫助將 NSXMLParser 與 ASIHTTPRequest 一起使用 (need help using NSXMLParser with ASIHTTPRequest)

I am currently trying to parse the string representation of my xml that I get back from my php script on my server which is passed to the ASIHTTPRequest method  *- (void)requestFinished:(ASIHTTPRequest )request 

I want to use NSXMLParser to parse this xml, so far I have got everything up and running please see code for how I have done this, however the issue I am having is that it seems to be accessing the *- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString )string{ method multiple times.. when I only want it to access it the one time and return the value. any ideas on why this is happening would be greatly appreciated.

- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text
    NSString *responseString = [request responseString];
    NSData *xData = [responseString dataUsingEncoding:NSUTF8StringEncoding];
    //myCode.text = responseString;
    //NSLog(@" response %@", responseString);

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xData];

    [parser setDelegate:self];
    [parser parse];
    [parser release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    if ([elementName isEqual:@"code"]) {
        NSLog(@"Found title!");
        titleString = [[NSMutableString alloc] init];
    }
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    [titleString appendString:string];
    NSLog(@"the code string =%@", titleString);

}

//EDITTED:::::

I forgot to add this delegate method which is the last to be called and will output the final result. And the reason I said it was not working was because I wrote something wrong in the method name.... all fixed now and working perfectly.

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ([elementName isEqual:@"code"]) {
        //NSLog(@"ended title: %@", titleString);
        //Pass  code over to animation
        [self parseCodeForAnimation:titleString];

        [titleString release];
        titleString = nil;
    }   

}

參考解法

方法 1:

In general, SAX parsers call the foundCharacters method many times, depending in large part on the contents of the data being parsed. All you can really do is code your app in such a way that it handles this correctly. Typically, you do this by keeping something like an NSMutableData or NSMutableString around and appending to it in each invocation of foundCharacters. When you receive an end tag that signifies that no more data will be sent as part of this element, you dump the mutable contents into a string, store it, and reset the buffer for the next set of data.

(by C.Johnsdeong)

參考文件

  1. need help using NSXMLParser with ASIHTTPRequest (CC BY-SA 3.0/4.0)

#asihttprequest #nsxmlparser #iOS






相關問題

遠程服務器上的請求總是失敗 (Request always fail on distant server)

ASIHTTPRequest 緩存我的數據。我不想要任何類型的緩存 (ASIHTTPRequest caches my data. I don't want any kind of cache)

在調用之前釋放了 didFinishSelector ASIHTTPRequest 的自定義委託類 (Custom delegate class for didFinishSelector ASIHTTPRequest being deallocated before called)

Android:在後台上傳/下載文件數量的最佳方式 (Android: best way to upload/download number of files in background)

Ghi giá trị bài đăng trong ASIFormDataRequest (Log Post Value in ASIFormDataRequest)

ASIHTTPRequest - 下載問題 (ASIHTTPRequest - download problem)

ASIHTTPRequest 離線模式連接失敗 (ASIHTTPRequest offline mode connection failure)

ASIHttpRequest 請求:didReceiveBytes:未按預期工作 (ASIHttpRequest request:didReceiveBytes: not working as expected)

如何讓 Ruby on Rails 與 ASIHTTPRequest 一起使用? (How do I get Ruby on Rails to work with ASIHTTPRequest?)

可以指定一個 NSObject 作為多個 ASIHTTPRequests 的代表嗎? (OK to assign one NSObject as the delegate of multiple ASIHTTPRequests?)

需要幫助將 NSXMLParser 與 ASIHTTPRequest 一起使用 (need help using NSXMLParser with ASIHTTPRequest)

處理 ASIHTTPRequest 多個請求 (Handling ASIHTTPRequest Multiple Requests)







留言討論