回答
的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]
感谢您的回复 – SampathKumar 2012-11-03 06:17:01
乐意帮助:) – Nina 2012-11-05 07:13:00
但如果XML文件包含标签中的属性呢?可以将结果映射到.plist文件中吗? – dan 2013-08-06 10:43:18
首先,你需要加载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];
添加新的文件(的plist)
源代码格式打开它
复制粘贴XML代码将工作,如果你的代码是正确的
实际上,我们写的基础上的NSXMLParser一个简单的解析类,使用存在于NSXMLParserDelegate方法走过的数据,并返回一个字典。
然后这个字典可以保存为一个文件,这将是一个pList。
这导致直接的XML - > pList数据转换,其中pList是XML的精确表示。
我不知道为什么Xcode没有这个确切的东西作为示例文件,因为它看起来像这样一个共同的需要。
如果NSPropertyListSerialization propertyListFromData:plistXML替换了所有这些,那么我将再次看看我们的代码。
是的。
在xcode中打开plist文件。 复制plist文件。现在你将有一个提示窗口。选择属性列表XML从格式下降。 添加.xml作为扩展名。
并享受:-D
- 1. Swift - 如何将普通的XML文件转换为.plist XML文件
- 2. 转换文本文件,XML在java中
- 3. XML文件转换
- 4. 转换文本文件,XML文件
- 5. 在.plist文件中写入iphone/ipad?
- 6. iPhone读/写.plist文件
- 7. 写/阅读plist文件iPhone
- 8. 将NSString从plist文件转换为NSURL
- 9. 将plist xml文件转换为mac上的html/pdf格式
- 10. 在c#中转换excel文件在xml文件中#
- 11. C#替换.plist/XML文件中的某些文本?
- 12. 从XML文件转换datestamp
- 13. 用Java转换XML文件
- 14. 转换大型XML文件
- 15. 转换.CSV文件到.XML
- 16. XSLT:转换XML文件树
- 17. 转换XML响应文件
- 18. 在iPhone中将HTML文件转换为PDF文件
- 19. 如何在iphone中将jpeg文件转换为pdf文件?
- 20. 将文本文件转换为XML
- 21. 将xml文件转换为文本
- 22. 将文本转换成xml文件
- 23. 使用XSLT将XML文件转换为另一个XML文件
- 24. 转换,从XML到PList
- 25. iPhone - 我们可以使用NSXMLParser解析.plist(xml)文件吗?
- 26. 转换HTML文件为PDF文件中的iphone
- 27. iPhone上的pList文件的限制
- 28. iPhone:无法从文件的.plist
- 29. 远程写入.plist文件iPhone SDK
- 30. iphone定时器plist文件显示
plist已经是XML。你为什么需要转换它? – CodaFi 2012-07-18 12:45:29
因为,现在我在android应用程序中工作,所以xml文件想要它 – SampathKumar 2012-07-18 12:46:44
不。它已经是XML了,但是对于你的想法来说这是错误的XML。这是一种专有的Apple格式。不好意思,朋友。 – CodaFi 2012-07-18 12:47:26