2012-07-18 60 views
0

是否有可能xml文件从plist中转换,如何转换这一点,请任何一个帮助我Plist文件在iphone中转换xml文件?

感谢

+1

plist已经是XML。你为什么需要转换它? – CodaFi 2012-07-18 12:45:29

+0

因为,现在我在android应用程序中工作,所以xml文件想要它 – SampathKumar 2012-07-18 12:46:44

+1

不。它已经是XML了,但是对于你的想法来说这是错误的XML。这是一种专有的Apple格式。不好意思,朋友。 – CodaFi 2012-07-18 12:47:26

回答

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] 
+0

感谢您的回复 – SampathKumar 2012-11-03 06:17:01

+0

乐意帮助:) – Nina 2012-11-05 07:13:00

+0

但如果XML文件包含标签中的属性呢?可以将结果映射到.plist文件中吗? – dan 2013-08-06 10:43:18

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]; 
1

添加新的文件(的plist)

源代码格式打开它

复制粘贴XML代码将工作,如果你的代码是正确的

0

实际上,我们写的基础上的NSXMLParser一个简单的解析类,使用存在于NSXMLParserDelegate方法走过的数据,并返回一个字典。

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

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

我不知道为什么Xcode没有这个确切的东西作为示例文件,因为它看起来像这样一个共同的需要。

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

0

是的。

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

并享受:-D