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

发布于 2025-01-01 00:32:45 字数 4940 浏览 2 评论 0原文

我有一个单独的类来解析从服务器获取的 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 没有给我任何结果。相反,当我在控制器类中使用相同的代码时,我得到了所需的结果,我想知道为什么。

I have a separate class for parsing XML I am getting from server. Here is my model class :

#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

When I debug my application and reach didEndElement, soapResult gives me nothing. On the contrary, when I use the same code in my controller class, I get the desired results, I wonder why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光①夏 2025-01-08 00:32:45

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

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

You're not implementing parser:foundCharacters: I see that your allocating the string in start element but you would need to get the data out and set soapResults in foundCharacters. Where do you assign to soapResults? I don't see any code that ever assigns it which is why it's nil.

Also, didEndElement fires whenever the parser reaches the end of an element - not when it's done parsing. That would be parserDidEndDocument. So, it's possible that the parser hit the end of an element but still hasn't passed the two elements your interested in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文