parserDidEndDocument没有被调用

时间:2011-06-18 22:03:41

标签: iphone xml xcode xcode4 nsxmlparser

parserDidNotEndDocument没有被调用,它在昨天工作,我认为唯一不同的是我现在正在使用xcode 4。抛出错误是XML Parse错误:操作无法完成。 (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);

}

1 个答案:

答案 0 :(得分:2)

查找解析错误的解决方案:

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