以前,我能够通过这种编码获取 URL 的内容:
<?xml version = "1.0" encoding = "UTF-8"?>
并将其解析为 NSString,代码如下:
NSMutableData *receivedData = [[[NSMutableData alloc] initWithContentsOfURL:[NSURL URLWithString:authenticateURL]] autorelease];
NSString *string = [[[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding] autorelease];if (string) { // if connection established, string will look for <error> in XML
NSRange range = [string rangeOfString:@"<error>"];
if (range.location == NSNotFound) {
result = TRUE;
NSLog(@"Authenticated!"); // if <error> not found, record is present
} //return TRUE to load data
else {
result = FALSE; // <error> found in XML, record is not found
NSLog(@"Not Authenticated!");
}
}
但是,我现在需要获取的内容实际上是没有编码类型的,例如:
<?xml version="1.0" ?><authenticate><auth>Y</auth></authenticate>
因此现在,当我使用 NSLog 字符串时,它是空的。尝试搜索其他方法,但我发现的所有示例,人们都可以使用 UTF8StringEncoding。很乐意提供任何建议:)!谢谢!
最佳答案
您可以将开源库 TBXML 与 https://gist.github.com/1922643 结合使用将接收到的 XML 转换为 NSDictionary。它支持 NSData(使用 NSUTF8StringEncoding)和纯 NSStrings。
这个 NSDictionary 包含 XML 中可用的所有信息,您可以使用 objectForKey: 函数读取它。
关于iphone - 将 XML 转换为 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12050649/