gpt4 book ai didi

iphone - 无法解析 XML iPhone

转载 作者:行者123 更新时间:2023-11-29 11:18:26 27 4
gpt4 key购买 nike

我正在使用下面的代码来解析下载的文件,之前在重新启动我的 mac 之前它工作正常但现在它说无法解析 XML,而需要下载的数据正在正确显示。

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

最佳答案

如果无法解析 XML,您应该查看 -(NSError *)parseError 以查看错误的确切位置。

NSLog(@"Parse Error: %@", [self.xmlParser parseError]);

这不是您问题的直接答案,但由于您实际上没有问过(),我不知道从哪里开始。

关于iphone - 无法解析 XML iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8371289/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com