【问题标题】:Plist file to convert xml file in iphone?Plist文件在iphone中转换xml文件?
【发布时间】:2012-07-18 12:37:34
【问题描述】:

是否可以从 plist 转换 xml 文件,如何转换?

【问题讨论】:

  • 一个 plist 已经是 XML。为什么需要转换它?
  • 因为,现在我在android应用程序中工作,所以xml文件需要它
  • 不。它已经是 XML,但是对于您的想法来说,它是一种错误的 XML。这是一种专有的 Apple 格式。对不起,伙计。
  • 好的,然后在iphone应用程序中保存一个xml文件是否可以

标签: iphone ios xml plist


【解决方案1】:

plist 是一个 XML 格式的文件。您可以使用以下方法将数据从 plist 存储到字典 :-) 然后您可以使用正确的键值获取值!!

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"example.plist"];

NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format; 

NSMutableDictionary *tempDict = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];

//如果你想保存它

[tempDict writeToFile:plistPath atomically:YES]

【讨论】:

  • 但是如果 XML 文件在标签中包含属性怎么办?是否可以在结果 .plist 文件中映射它们?
  • 只是另一对键值对。应该可以的
【解决方案2】:

首先,您需要将 plist 文件加载到 NSDictionary,这样:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"plist"];;
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:filePath]; //or use dictionaryWithContentsOfURL to get from the web

然后将其转换为 NSData

NSData *xmlData = [NSPropertyListSerialization dataFromPropertyList:dictionary format:NSPropertyListXMLFormat_v1_0   errorDescription:nil];
NSString *xmlString = [[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding];

【讨论】:

    【解决方案3】:

    添加新文件(plist)

    以源代码格式打开

    复制粘贴 xml 代码,如果您的代码正确,它将起作用

    【讨论】:

      【解决方案4】:

      我们实际上基于 NSXMLParser 编写了一个简单的解析器类,使用 NSXMLParserDelegate 中的方法遍历数据并返回一个字典。

      然后这个字典可以保存为一个文件,这将是一个pList。

      这导致直接的 XML -> pList 数据转换,其中 pList 是 XML 的精确表示。

      我不知道为什么 Xcode 没有这个确切的东西作为示例文件,因为它看起来很常见。

      如果 NSPropertyListSerialization propertyListFromData:plistXML 取代了所有这些,那么我将再看看我们的代码。

      【讨论】:

        【解决方案5】:

        是的。

        在 xcode 中打开 plist 文件。 制作 plist 文件的副本。现在您将有一个提示窗口。从 Format 下拉菜单中选择 Property List XML。 添加 .xml 作为扩展名。

        并享受:-D

        【讨论】:

          猜你喜欢
          • 2016-12-30
          • 1970-01-01
          • 2012-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-21
          • 1970-01-01
          相关资源
          最近更新 更多