iphone - 将 XML 转换为 NSString

标签 iphone ios xml encoding nsmutabledata

以前,我能够通过这种编码获取 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/

相关文章:

ios - 设备 token 在开发中注册良好,但在生产中没有

Android - 通过引用其他定义的颜色来定义颜色

ios - 在 ScrollView 中隐藏 View 后删除空格?

ios - Callkit 回拨按钮不起作用

iphone - 访问 UIDatePicker 时的 EXC_BAD_ACCESS

mysql - 详细的 RoR 应用程序...带有 MySQL 的 XML 或仅 MySQL

xml - 如何使用 XPath 根据条件获取包含默认命名空间的 XML 中的特定元素

iphone - 如何在 iPhone 上使用 NSXML 解析器获取所有值

android - 如何比较 iOS 和 Android 中的加速度计值

ios - iOS SDK Skype + 中的延迟问题连接完成时默认音频源静音