需要帮助将NSXMLParser与ASIHTTPRequest一起使用 [英] need help using NSXMLParser with ASIHTTPRequest

查看:97
本文介绍了需要帮助将NSXMLParser与ASIHTTPRequest一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试解析从服务器上的php脚本获取的xml字符串表示形式,该字符串表示形式已传递给ASIHTTPRequest方法 *-(void)requestFinished:(ASIHTTPRequest )request

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

我想使用NSXMLParser解析此xml,到目前为止,我已经启动并运行了所有程序,请参见代码来了解如何执行此操作,但是我遇到的问题是,它似乎正在访问*-(无效)parser:(NSXMLParser *)parser foundCharacters:(NSString )string {方法多次..当我只希望它一次访问它并返回值时.对此原因的任何想法将不胜感激.

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);

}

//EDITED :::::

//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;
    }   

}

推荐答案

通常,SAX解析器会多次调用foundCharacters方法,这在很大程度上取决于要解析的数据的内容.您真正能做的就是以一种正确处理应用程序的方式编写应用程序代码.通常,您可以通过在每次调用FoundCharacters时保留NSMutableData或NSMutableString之类的内容并将其附加在其周围来完成此操作.当您收到一个结束标记,表示该元素的一部分将不再发送任何数据时,请将可变内容转储到字符串中,进行存储,然后为下一组数据重置缓冲区.

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.

这篇关于需要帮助将NSXMLParser与ASIHTTPRequest一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆