how to write to XML file in objective c cocoa

1k Views Asked by At

m new to xcode and objective c cocoa framework and i want to write xml data to xml file placed in my cocoa project but i want that once i created xml file and when i closes application and open it again to create xml file then it does not overwrite it but should append new data to previous one. e.g in following example when i create xml, close application and open it again then new children1s and children2 should be appended to previous one.

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

There are 0 best solutions below