我是 xcode 和目标 c 可可框架的新手,我想将 xml 数据写入放置在我的可可项目中的 xml 文件,但是我希望一旦我创建了 xml 文件并且当我关闭应用程序并再次打开它以创建 xml 文件然后它不会覆盖它,但应该将新数据附加到前一个。例如,在下面的示例中,当我创建 xml 时,关闭应用程序并再次打开它,然后新的 children1s 和 children2 应该附加到前一个。
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];