parserDidEndDocument 没有被调用

发布于 2024-11-16 06:07:58 字数 7534 浏览 1 评论 0原文

parserDidNotEndDocument 没有被调用,它昨天还在工作,我认为唯一不同的是我现在使用的是 xcode 4。抛出的错误是 XML 解析错误:操作无法完成。 (NSXMLParserErrorDomain 错误 5。)

@implementation profileViewController
@synthesize userArray;
@synthesize userData;
@synthesize userId;
@synthesize gender;

@synthesize nameIB;
@synthesize numberIB;
@synthesize loginButton;
@synthesize logoutButton;
@synthesize activityIndicator;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

-(IBAction)textDone:(id)sender
{
    [sender resignFirstResponder];
}

-(IBAction)genderSelection:(id)sender
{
        if([sender selectedSegmentIndex] == kSwitchesSegmentIndex)
        {
                gender = @"Male";
                //NSLog(gender);
        }
        else {
            gender = @"Female";
            //NSLog(gender);
        }

}

-(IBAction)postData:(id)sender
{      
    loginButton.hidden = YES;
    loginButton.enabled = NO;

    if(nameIB.text.length && numberIB.text.length > 0)
    {
        //[self performSelectorInBackground:@selector(sendData) withObject:nil];
        NSMutableData *data = [NSMutableData data]; 

        NSString *number = numberIB.text;
        NSString *name = nameIB.text;

        NSString *nameString = [[NSString alloc] initWithFormat:@"name=%@", name];
        NSString *numberString = [[NSString alloc] initWithFormat:@"&number=%@", number];
        NSString *genderString = [[NSString alloc] initWithFormat:@"&gender=%@", gender];

        //NSLog(nameString);
        //NSLog(numberString);

        [data appendData:[nameString dataUsingEncoding:NSUTF8StringEncoding]];
        [data appendData:[numberString dataUsingEncoding:NSUTF8StringEncoding]];
        [data appendData:[genderString dataUsingEncoding:NSUTF8StringEncoding]];

        NSURL *url = [NSURL URLWithString:@"http://www.blah.net/test.php"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:data];

        NSURLResponse *response;
        NSError *err;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
        NSLog(@"responseData: %@", responseData);

        userData = responseData;

        logoutButton.hidden = NO;
        logoutButton.enabled = YES;

        [self startParsingUserId];

    }
    else 
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Text Fields Empty" message:@"One Or More Textfields Are Empty" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert show];
        [alert release];

        loginButton.enabled = YES;
        loginButton.hidden = NO;
        logoutButton.enabled = NO;

    }


}

/*-(void)sendData
{
    [activityIndicator startAnimating];
    NSMutableData *data = [NSMutableData data]; 

    NSString *number = numberIB.text;
    NSString *name = nameIB.text;

    NSString *nameString = [[NSString alloc] initWithFormat:@"name=%@", name];
    NSString *numberString = [[NSString alloc] initWithFormat:@"&number=%@", number];
    NSString *genderString = [[NSString alloc] initWithFormat:@"&gender=%@", gender];

    //NSLog(nameString);
    //NSLog(numberString);

    [data appendData:[nameString dataUsingEncoding:NSUTF8StringEncoding]];
    [data appendData:[numberString dataUsingEncoding:NSUTF8StringEncoding]];
    [data appendData:[genderString dataUsingEncoding:NSUTF8StringEncoding]];

    NSURL *url = [NSURL URLWithString:@"http://www.blah.net/test.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:data];

    NSURLResponse *response;
    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    NSLog(@"responseData: %@", responseData);

    userData = responseData;

}*/

-(IBAction)logout:(id)sender
{
    NSString *userID = self.userId;

    NSMutableData *data = [NSMutableData data]; 

    NSMutableString *userString = [[NSMutableString alloc] initWithFormat:@"id=%@", userID];

    //NSLog(userString);
    //NSLog(numberString);

    [data appendData:[userString dataUsingEncoding:NSUTF8StringEncoding]];

    NSURL *url = [NSURL URLWithString:@"http://www.blah.net/offline.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:data];

    NSURLResponse *response;
    NSError *err;

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    NSLog(@"responseData: %@", responseData);

    loginButton.hidden = NO;
    logoutButton.hidden = YES;

    loginButton.enabled = YES;
    logoutButton.enabled = NO;

}

-(void)startParsingUserId
{
    NSLog(@"parsing init");
    NSXMLParser *idParser = [[NSXMLParser alloc] initWithData:userData]; //uses the NSMutableData data type to parse
    idParser.delegate = self; //set the delegate to this viewControlelr
    [idParser parse];
    [idParser release];
}

-(void)parserDidStartDocument:(NSXMLParser *)parser
{
    NSLog(@"parsing started");

    currentElementName = nil;
    currentText = nil;
}

//this is called for each xml element
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    //NSLog(@"started element");
    if ([elementName isEqualToString:@"usercallback"]) //if elementName == status then start of new tweet so make new dictionary
    {
        [currentIDDict release];
        currentIDDict = [[NSMutableDictionary alloc] initWithCapacity:[interestingTags count]]; //make dictionary with two sections
    }
    else if([interestingTags containsObject:elementName]) //if current element is one stored in interesting tag, hold onto the elementName and make a new string to hold its value
    {
        currentElementName = elementName; //hold onto current element name
        currentText = [[NSMutableString alloc] init];
    }

}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    NSLog(@"appending");
    [currentText appendString:string];
}

//after each element it goes back to the parent after calling this method
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:currentElementName])
    {
        [currentIDDict setValue: currentText forKey: currentElementName];
    }
    else if([elementName isEqualToString:@"usercallback"])
    {
        [self.userArray addObject:currentIDDict];

    }

    NSLog(@"ending");
    currentText = nil;
    [currentText release];
}

-(void)parserDidEndDocument:(NSXMLParser *)parser
{
    NSLog(@"end");
    NSDictionary *rowData = [self.userArray objectAtIndex:0];
    userId = [[NSString alloc] initWithFormat:@"%@", [rowData objectForKey:@"id"]];

    NSLog(@"DONE PARSING DOCUMENT");
    NSLog(@"userid = %@", userId);

}

parserDidNotEndDocument does not get called, it was working yesterday, the only thing that i think is different is i am using xcode 4 now. error getting thrown is Error on XML Parse: The operation couldn’t be completed. (NSXMLParserErrorDomain error 5.)

@implementation profileViewController
@synthesize userArray;
@synthesize userData;
@synthesize userId;
@synthesize gender;

@synthesize nameIB;
@synthesize numberIB;
@synthesize loginButton;
@synthesize logoutButton;
@synthesize activityIndicator;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

-(IBAction)textDone:(id)sender
{
    [sender resignFirstResponder];
}

-(IBAction)genderSelection:(id)sender
{
        if([sender selectedSegmentIndex] == kSwitchesSegmentIndex)
        {
                gender = @"Male";
                //NSLog(gender);
        }
        else {
            gender = @"Female";
            //NSLog(gender);
        }

}

-(IBAction)postData:(id)sender
{      
    loginButton.hidden = YES;
    loginButton.enabled = NO;

    if(nameIB.text.length && numberIB.text.length > 0)
    {
        //[self performSelectorInBackground:@selector(sendData) withObject:nil];
        NSMutableData *data = [NSMutableData data]; 

        NSString *number = numberIB.text;
        NSString *name = nameIB.text;

        NSString *nameString = [[NSString alloc] initWithFormat:@"name=%@", name];
        NSString *numberString = [[NSString alloc] initWithFormat:@"&number=%@", number];
        NSString *genderString = [[NSString alloc] initWithFormat:@"&gender=%@", gender];

        //NSLog(nameString);
        //NSLog(numberString);

        [data appendData:[nameString dataUsingEncoding:NSUTF8StringEncoding]];
        [data appendData:[numberString dataUsingEncoding:NSUTF8StringEncoding]];
        [data appendData:[genderString dataUsingEncoding:NSUTF8StringEncoding]];

        NSURL *url = [NSURL URLWithString:@"http://www.blah.net/test.php"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:data];

        NSURLResponse *response;
        NSError *err;
        NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
        NSLog(@"responseData: %@", responseData);

        userData = responseData;

        logoutButton.hidden = NO;
        logoutButton.enabled = YES;

        [self startParsingUserId];

    }
    else 
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Text Fields Empty" message:@"One Or More Textfields Are Empty" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert show];
        [alert release];

        loginButton.enabled = YES;
        loginButton.hidden = NO;
        logoutButton.enabled = NO;

    }


}

/*-(void)sendData
{
    [activityIndicator startAnimating];
    NSMutableData *data = [NSMutableData data]; 

    NSString *number = numberIB.text;
    NSString *name = nameIB.text;

    NSString *nameString = [[NSString alloc] initWithFormat:@"name=%@", name];
    NSString *numberString = [[NSString alloc] initWithFormat:@"&number=%@", number];
    NSString *genderString = [[NSString alloc] initWithFormat:@"&gender=%@", gender];

    //NSLog(nameString);
    //NSLog(numberString);

    [data appendData:[nameString dataUsingEncoding:NSUTF8StringEncoding]];
    [data appendData:[numberString dataUsingEncoding:NSUTF8StringEncoding]];
    [data appendData:[genderString dataUsingEncoding:NSUTF8StringEncoding]];

    NSURL *url = [NSURL URLWithString:@"http://www.blah.net/test.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:data];

    NSURLResponse *response;
    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    NSLog(@"responseData: %@", responseData);

    userData = responseData;

}*/

-(IBAction)logout:(id)sender
{
    NSString *userID = self.userId;

    NSMutableData *data = [NSMutableData data]; 

    NSMutableString *userString = [[NSMutableString alloc] initWithFormat:@"id=%@", userID];

    //NSLog(userString);
    //NSLog(numberString);

    [data appendData:[userString dataUsingEncoding:NSUTF8StringEncoding]];

    NSURL *url = [NSURL URLWithString:@"http://www.blah.net/offline.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:data];

    NSURLResponse *response;
    NSError *err;

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    NSLog(@"responseData: %@", responseData);

    loginButton.hidden = NO;
    logoutButton.hidden = YES;

    loginButton.enabled = YES;
    logoutButton.enabled = NO;

}

-(void)startParsingUserId
{
    NSLog(@"parsing init");
    NSXMLParser *idParser = [[NSXMLParser alloc] initWithData:userData]; //uses the NSMutableData data type to parse
    idParser.delegate = self; //set the delegate to this viewControlelr
    [idParser parse];
    [idParser release];
}

-(void)parserDidStartDocument:(NSXMLParser *)parser
{
    NSLog(@"parsing started");

    currentElementName = nil;
    currentText = nil;
}

//this is called for each xml element
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
    //NSLog(@"started element");
    if ([elementName isEqualToString:@"usercallback"]) //if elementName == status then start of new tweet so make new dictionary
    {
        [currentIDDict release];
        currentIDDict = [[NSMutableDictionary alloc] initWithCapacity:[interestingTags count]]; //make dictionary with two sections
    }
    else if([interestingTags containsObject:elementName]) //if current element is one stored in interesting tag, hold onto the elementName and make a new string to hold its value
    {
        currentElementName = elementName; //hold onto current element name
        currentText = [[NSMutableString alloc] init];
    }

}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    NSLog(@"appending");
    [currentText appendString:string];
}

//after each element it goes back to the parent after calling this method
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:currentElementName])
    {
        [currentIDDict setValue: currentText forKey: currentElementName];
    }
    else if([elementName isEqualToString:@"usercallback"])
    {
        [self.userArray addObject:currentIDDict];

    }

    NSLog(@"ending");
    currentText = nil;
    [currentText release];
}

-(void)parserDidEndDocument:(NSXMLParser *)parser
{
    NSLog(@"end");
    NSDictionary *rowData = [self.userArray objectAtIndex:0];
    userId = [[NSString alloc] initWithFormat:@"%@", [rowData objectForKey:@"id"]];

    NSLog(@"DONE PARSING DOCUMENT");
    NSLog(@"userid = %@", userId);

}

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

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

发布评论

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

评论(1

潦草背影 2024-11-23 06:07:58

发现解析错误的解决方案:

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSDictionary *userInfo = parseError.userInfo;
    NSNumber *lineNumber   = userInfo[@"NSXMLParserErrorLineNumber"];
    NSNumber *errorColumn  = userInfo[@"NSXMLParserErrorColumn"];
    NSString *errorMessage = userInfo[@"NSXMLParserErrorMessage"];

    NSLog(@"Error occured in line %@ and column %@\nWith message: %@", lineNumber, errorColumn, errorMessage);
}

Solution for finding parse error:

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSDictionary *userInfo = parseError.userInfo;
    NSNumber *lineNumber   = userInfo[@"NSXMLParserErrorLineNumber"];
    NSNumber *errorColumn  = userInfo[@"NSXMLParserErrorColumn"];
    NSString *errorMessage = userInfo[@"NSXMLParserErrorMessage"];

    NSLog(@"Error occured in line %@ and column %@\nWith message: %@", lineNumber, errorColumn, errorMessage);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文