0

私はxcodeと目的のcココアフレームワークを初めて使用し、ココアプロジェクトのリソースフォルダーに配置されたxmlファイルにxmlデータを書き込みたいのですが、ファイルにデータを書き込むことができません。誰でも助けてください。

NSXMLElement *root = [[NSXMLElement alloc] initWithName:@"Request"];
[root addAttribute:[NSXMLNode attributeWithName:@"Attribute1" stringValue:@"Value1"]];
[root addAttribute:[NSXMLNode attributeWithName:@"Attribute2" stringValue:@"Value2"]];
[root addAttribute:[NSXMLNode attributeWithName:@"Attribute3" stringValue:@"Value3"]];

NSXMLElement *childElement1 = [[NSXMLElement alloc] initWithName:@"ChildElement1"];
[root addChild:childElement1];
[childElement1 release];

NSXMLElement *childElement2 = [[NSXMLElement alloc] initWithName:@"ChildElement2"];
[childElement2 addAttribute:[NSXMLNode attributeWithName:@"ChildAttribute2.1" stringValue:@"Value2.1"]];
[childElement2 setStringValue:@"ChildValue2.1"];
[root addChild:childElement2];
[childElement2 release];

NSXMLDocument *xmlRequest = [NSXMLDocument documentWithRootElement:root];
[root release];
NSLog(@"XML Document\n%@", xmlRequest);//till this art code runs fine.
but when next part starts i got bad exception and code stops.
NSData *xmlData = [xmlRequest XMLDataWithOptions:NSXMLNodePrettyPrint];
[xmlData writeToFile:@"/Users/halen/Documents/project3/xmlsample.xml" atomically:YES];
[xmlRequest release];

xml ファイルの配置に問題があるかどうかはわかりません。xmlファイルをどこに配置しますか?? そして、xml データを .xml ファイルに書き込む正しい方法は何ですか?

4

2 に答える 2

0

writeToFile: パスが必要です。有効なパスが指定されていません。

于 2013-12-25T10:58:48.443 に答える
0

この方法を試してください

[[NSFileManager defaultManager] createFileAtPath:@"/Some/Path/foo.xml"
                            contents:xmlData
                            attributes:nil];
于 2013-12-25T10:59:51.777 に答える