Hi, I have a big problem. My program must save a Xml file with 3 float value (x, y, z of accelerometer) and a NSString value (name). How can I do? Thanks so much
+1
A:
If you don't have specific requirement on the XML format, then you can use the NSKeyedArchiver class to archive the data in XML format:
float x, y, z;
NSString *name;
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver setOutputFormat:NSPropertyListXMLFormat_v1_0];
[archiver encodeFloat:x forKey:@"x"];
[archiver encodeFloat:y forKey:@"y"];
[archiver encodeFloat:z forKey:@"z"];
[archiver encodeObject:name forKey:@"name"];
[archiver finishEncoding];
BOOL result = [data writeToFile:@"MyFile" atomically:YES];
[archiver release];
Once written, the reading is simple:
NSData *data = [NSData dataWithContentsOfFile:@"MyFile"];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
x = [unarchiver decodeFloatForKey:@"x"];
y = [unarchiver decodeFloatForKey:@"y"];
z = [unarchiver decodeFloatForKey:@"z"];
name = [unarchiver decodeObjectForKey:@"name"];
[unarchiver finishDecoding];
[unarchiver release];
Laurent Etiemble
2010-05-26 20:35:52
Thanks so much.I have a question, where is stored the file?
zp26
2010-05-27 16:38:32
It can be wherever you want. For example, you can store it in the application support folder (see http://cocoawithlove.com/2010/05/finding-or-creating-application-support.html).
Laurent Etiemble
2010-05-27 17:53:33