plistからxmlファイルを変換することは可能ですか、これを変換する方法は?
5 に答える
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]
まず、次のように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