1
我正在使用下面的代碼來解析下載的文件,以前在重新啓動我的mac之前它工作正常,但現在它說無法解析XML,而需要下載的數據正在顯示正常。無法解析XML iPhone
-(IBAction)loginButton:(id)sender{
NSString *user = [userNameField text];
NSString *passwd = [passwordField text];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:user forKey:@"Username"];
[defaults setObject:passwd forKey:@"Password"];
[defaults synchronize];
NSLog(@"Data Saved");
NSString *urlAsString = @"http";
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"POST"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response,NSData *data, NSError *error) {
if ([data length] >0 && error == nil){
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HTML = %@", html);
}
else if ([data length] == 0 && error == nil){
NSLog(@"Nothing was downloaded.");
}
else if (error != nil){
NSLog(@"Error happened = %@", error);
} }];
NSString *xmlFilePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"xml"];
NSData *xml = [[NSData alloc] initWithContentsOfFile:xmlFilePath];
self.xmlParser = [[NSXMLParser alloc] initWithData:xml];
self.xmlParser.delegate = self;
if ([self.xmlParser parse]) {
NSLog(@"The XML is parsed");
}else{
NSLog(@"Failed to parse the XML");
}
}
-(void)parserDidStartDocument:(NSXMLParser *)parser {
self.rootElement = nil;
self.currentElementPointer = nil; }
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
if (self.rootElement == nil){
self.rootElement = [[XMLElement alloc] init]; self.currentElementPointer = self.rootElement;
} else {
XMLElement *newElement = [[XMLElement alloc] init]; newElement.parent = self.currentElementPointer; [self.currentElementPointer.subElements addObject:newElement]; self.currentElementPointer = newElement;
}
self.currentElementPointer.name = elementName; self.currentElementPointer.attributes = attributeDict;
NSString *user = [NSString stringWithFormat:@"%@", [attributeDict objectForKey:@"user"]];
NSLog(@"User: %@", user);
if ([user isEqualToString:@"1"]) {
NSLog(@"Test succeeded!");
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([self.currentElementPointer.text length] > 0){
self.currentElementPointer.text =
[self.currentElementPointer.text stringByAppendingString:string];
} else {
self.currentElementPointer.text = string; }
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
self.currentElementPointer = self.currentElementPointer.parent;
}
-(void)parserDidEndDocument:(NSXMLParser *)parser{
self.currentElementPointer = nil;
}
thanx的幫助!問題已經解決:) – AppDeveloper