在HTTP响应中获取XML并阅读它

时间:2012-02-07 11:18:14

标签: iphone objective-c ios xml

我有以下方法,我有一个方法,我通过它调用此方法,但没有运气呢。

- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
    namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict{

        if ([elementName isEqualToString:@"isphone-config"]) {
            Pusername = [attributeDict valueForKey:@"authentication_username"];    
            Ppassword = [attributeDict valueForKey:@"authentication_password"];
            Pidentity = [attributeDict valueForKey:@"sip_identity"];
            Pdomain   = [attributeDict valueForKey:@"domain"];
            Pprotocol = [attributeDict valueForKey:@"protocol"];
            Pudpport  = [attributeDict valueForKey:@"registrar_port"];
            Ptcpport  = [attributeDict valueForKey:@"listening_port"];

            NSLog(@"Title: %@, ID: %@", Pusername, Ppassword);
        }
    }
}

我用以下方法调用它。

NSString* username = [[NSUserDefaults standardUserDefaults] stringForKey:@"username_preference"];
NSString* accountPassword = [[NSUserDefaults standardUserDefaults] stringForKey:@"password_preference"];
NSString* urlString = [NSString stringWithFormat:@"https://%@:%@@test.com/test.php",username,accountPassword];
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

parser = [[NSXMLParser alloc] initWithData: response];

NSLog(@"at parser %@", parser);

[parser setDelegate:self];

我在调用解析器的代码的最后一行遇到问题。我假设这行将调用方法解析器,如果我错了,请纠正我。我不知道我必须导入,包含或实现哪些文件,接口和类。

1 个答案:

答案 0 :(得分:1)

使用NSXMLParser进行XML解析要困难得多,请查看Apple的“地震”示例。

相反,我建议你使用另一个XML解析器。例如,this one

Also, good article about how to choose parser.

http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml