【发布时间】:2012-08-21 08:09:13
【问题描述】:
以前,我可以使用这种编码获取 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。很高兴有任何建议:)!谢谢!
【问题讨论】:
-
你应该使用 NSXMLParser 类
-
@elppa 编辑了我的代码以显示我实际在做什么。嗯,我不想做所有的解析,我只想将 XML 转换为字符串,在那个字符串中我找到
,这更容易。因此,如果在该字符串中未发现错误,则该人将通过身份验证。 :) -
谢谢大家,我猜只是编码问题阻止了我检索代码
标签: iphone ios xml encoding nsmutabledata