0

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

4

5 回答 5

2

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]
于 2012-07-18T12:54:15.397 回答
1

添加新文件(plist)

以源代码格式打开

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

于 2012-07-18T12:53:37.257 回答
1

首先,您需要将 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];
于 2012-07-18T12:51:55.617 回答
0

是的。

在 xcode 中打开 plist 文件。制作 plist 文件的副本。现在您将有一个提示窗口。从格式下拉列表中选择属性列表 XML 。添加.xml作为扩展名。

并享受:-D

于 2015-02-04T12:11:52.387 回答
0

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

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

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

我不知道为什么 Xcode 没有这个确切的东西作为示例文件,因为它似乎是一种常见的需求。

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

于 2012-11-02T22:41:49.873 回答