iphone - 模型类中的 NSXMLParser 没有给我结果

标签 iphone ios nsxmlparser

我有一个单独的类来解析从服务器获取的 XML。这是我的模型类:

#import "CheckLoginModel.h"
#import "Common.h"
#import "Utils.h"
#import "Constants.h"

@implementation CheckLoginModel
@synthesize strUserID;
@synthesize strUserName;
@synthesize i;
@synthesize dict;

   -(void)CheckLogin:(NSString *)strDeviceToken
{
    dict = [[NSMutableDictionary alloc]init];
        @try
        {
            NSString *soapMessage = [NSString stringWithFormat:
                                     @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                                     "<soapenv:Envelope \n"
                                     "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" \n"
                                     "xmlns:tem=\"http://tempuri.org/\"> \n"
                                     "<soapenv:Header/>\n"
                                     "<soapenv:Body>\n"
                                     "<tem:CheckDeviceToken>\n"
                                     "<tem:dt>%@</tem:dt>\n"
                                     "</tem:CheckDeviceToken>\n"
                                     "</soapenv:Body>\n"
                                     "</soapenv:Envelope>\n",strDeviceToken];

            NSURL *url = [NSURL URLWithString:kMainURL]; 
            NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];             
            NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];          
            [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];       
            [theRequest addValue: @"http://tempuri.org/IService1/CheckDeviceToken" forHTTPHeaderField:@"Soapaction"];
            [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
            [theRequest setHTTPMethod:@"POST"];     
            [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
            NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
            if( theConnection )
            {
                webData = [[NSMutableData data] retain];
            }
            else
            {
                NSLog(@"The Connection is NULL");
            }
        }@catch (NSException *ex) {
            [Utils LogExceptionOnServer:@"ChatApplicationAppDelegate" methodName:@"CheckLogin" exception:[ex description]];
        }

}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR with theConenction");
    [connection release];
    [webData release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    NSLog(@"%@",theXML);
    [theXML release];

    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData: webData];
    [xmlParser setDelegate:self];
    [xmlParser setShouldResolveExternalEntities: YES];
    [xmlParser parse];
    [xmlParser release];

    [connection release];
    [webData release];

    //if(strUserName != NULL)
    [[NSNotificationCenter defaultCenter] postNotificationName:@"register" object:self userInfo:dict];
    //[dict release];

}

#pragma mark -
#pragma mark XML PARSING RELATED FUNCTIONS
#pragma mark -

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
   attributes: (NSDictionary *)attributeDict{

    if( [elementName isEqualToString:@"CheckDeviceTokenResult"])
    {

    }

    else if( [elementName isEqualToString:@"a:UserID"])
    {
        if(!soapResults)
            soapResults = [[NSMutableString alloc] init];

    }
    else if( [elementName isEqualToString:@"a:UserName"])
    {
        if(!soapResults)
            soapResults = [[NSMutableString alloc] init];

    }

}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{

    if( [elementName isEqualToString:@"a:UserID"])
    {
        i = [soapResults intValue];
        strUserID = soapResults;
        soapResults = nil;
        [dict setObject:strUserID forKey:@"id"];
    }
    else if( [elementName isEqualToString:@"a:UserName"])
    {
        strUserName = soapResults;
        soapResults = nil;
        [dict setObject:strUserName forKey:@"name"];

    }

}


@end

当我调试应用程序并到达 didEndElement 时,soapResult 没有给我任何信息。相反,当我在 Controller 类中使用相同的代码时,我得到了所需的结果,我想知道为什么。

最佳答案

您没有实现解析器:foundCharacters:我看到您在开始元素中分配了字符串,但您需要取出数据并在foundCharacters中设置soapResults。你在哪里分配给soapResults?我没有看到任何分配它的代码,这就是它为零的原因。

此外,只要解析器到达元素末尾,didEndElement 就会触发,而不是在解析完成时触发。那将是 parserDidEndDocument。因此,解析器可能到达了元素的末尾,但仍未传递您感兴趣的两个元素。

关于iphone - 模型类中的 NSXMLParser 没有给我结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9079779/

相关文章:

iphone - 使用 iPhone 相机的点云

ios - Cordova :GoogleService-info.plist 的问题

ios - iOS快速枚举时需要修改预加载数据的NSMutableArray

iphone - 使用 NSXMLParser 解析 XML 文件 - 获取值

iphone - 将自定义属性添加到MKAnnotation

ios - 如何在 ios 中通过 http get 请求在控制台中显示数据

ios - 如何在 iOS 中注册 GCM

ios - NavigationItem 的 titleView 动画不正确

ios - 如何在 Swift ios 中使用 JSON 输出解析 XML?

iphone - 如何在iOS中等待NSXMLParser完成