nsxmlparser问题

时间:2011-09-07 21:12:20

标签: iphone ios nsxmlparser

我几天来一直在与这个问题作斗争,并认为我找到了问题......

我使用ASIHTTPRequest方法连接到我的php脚本,该脚本调用来自数据库的请求数据,从那里我启动NSXMLParser委托一切似乎工作正常,我甚至可以NSLog结果,因为它们被解析但似乎当我尝试将这些结果放入NSMutableArray时,结果似乎不想进入它...我一直得到空值..

我希望有人可以给我一个理由,或者你是否可以指出我最终解决这个问题的方向,因为它成为背后真正的痛苦......

这是我的解析过程

- (IBAction)setRequestString:(NSString *)string
{
    //set up address
    NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"http://127.0.0.1:8888/CodeTest/"];
    [databaseURL appendString:string];
    //call delegates
    NSURL *url = [NSURL URLWithString:databaseURL]; //LIVE mode
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setDelegate:self];
    [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{     
    NSString *responseString = [request responseString]; //Pass request text from server over to NSString 
    NSData *capturedResponseData = [responseString dataUsingEncoding:NSUTF8StringEncoding]; 
    NSLog(@"%@", responseString);
    [self startTheParsingProcess:capturedResponseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@", error);
}





#pragma mark - Parsing lifecycle
//--- Start parsing process using NSXMLParser ---------------->>
- (void)startTheParsingProcess:(NSData *)parserData
{
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //incoming parserDatapassed to NSXMLParser delegate which starts parsing process 

    [parser setDelegate:self];
    [parser parse]; //Starts the event-driven parsing operation.
    [parser release];

    [self.tableView reloadData];

}

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

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [itemString appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqual:@"item"]) {
        //NSLog(@"ended title: %@", itemString);
        [myDataArray addObject:itemString];
        NSLog(@"%@", itemString);
        NSLog(@"%@", myDataArray);

        //TODO: Test release on memory consumption etc
        [itemString release];
        itemString = nil;
    }
}
//--- Finish parsing process using NSXMLParser ---------------->>

打印此

2011-09-08 08:46:24.424 iCode[1209:207] <?xml version="1.0"?>
<entries>
        <item>Honda</item>
        <item>Nissan</item>
        <item>Mitsubishi</item>
        <item>Toyota</item>
        <item>Mazda</item>
</entries>
2011-09-08 08:46:24.426 iCode[1209:207] Honda
2011-09-08 08:46:24.426 iCode[1209:207] (null)
2011-09-08 08:46:24.427 iCode[1209:207] Nissan
2011-09-08 08:46:24.427 iCode[1209:207] (null)
2011-09-08 08:46:24.428 iCode[1209:207] Mitsubishi
2011-09-08 08:46:24.428 iCode[1209:207] (null)
2011-09-08 08:46:24.430 iCode[1209:207] Toyota
2011-09-08 08:46:24.431 iCode[1209:207] (null)
2011-09-08 08:46:24.431 iCode[1209:207] Mazda
2011-09-08 08:46:24.432 iCode[1209:207] (null)

2 个答案:

答案 0 :(得分:1)

我想你会发现你的ivar myDataArray是零。这是因为你从未初始化它。

答案 1 :(得分:1)

您的myDataArray是nil,您只需将其分配到startTheParsingProcess:之类的地方。

示例:

- (void)startTheParsingProcess:(NSData *)parserData
{
    //You could release and create a new array like this example
    //or check if nil, and if nil create the array else remove all objects.
    [myDataArray release];
    myDataArray = [[NSMutableArray alloc] init];

    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:parserData]; //incoming parserDatapassed to NSXMLParser delegate which starts parsing process 

    [parser setDelegate:self];
    [parser parse]; //Starts the event-driven parsing operation.
    [parser release];

    [self.tableView reloadData];
}