views:

35

answers:

1

Hi, I have a problem with my code. I want to create a XML file like this:

<name>myName<name>
 <x>myX<x>
 <y>myY<y>
 <z>myZ<z>

but my file is:

<name>myName<name><x>myX<x><y>myY<y><z>myZ<z>

Can i have this text formatting? This is my code:

-(void)salvataggioInXML:(NSString*)name:(float)x:(float)y:(float)z{

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
 NSString *documentsDirectoryPath = [paths objectAtIndex:0];
 NSString *filePath = [documentsDirectoryPath  stringByAppendingPathComponent:@"filePosizioni.xml"];
 NSFileHandle *myHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
 [myHandle seekToEndOfFile];

 NSString *tagName = [NSString stringWithFormat:@"<name>%@<name>", name];
 NSString *tagX = [NSString stringWithFormat:@"<x>%f<x>", name];
 NSString *tagY = [NSString stringWithFormat:@"<y>%f<y>", name];
 NSString *tagZ = [NSString stringWithFormat:@"<z>%f<z>", name];

 NSData* dataName = [tagName dataUsingEncoding: NSASCIIStringEncoding];
 NSData* dataX = [tagX dataUsingEncoding: NSASCIIStringEncoding];
 NSData* dataY = [tagY dataUsingEncoding: NSASCIIStringEncoding];
 NSData* dataZ = [tagZ dataUsingEncoding: NSASCIIStringEncoding];

 [myHandle writeData:dataName];
 NSLog(@"writeok");

 [myHandle seekToEndOfFile];
 [myHandle writeData:dataX];
 NSLog(@"writeok");

 [myHandle seekToEndOfFile];
 [myHandle writeData:dataY];
 NSLog(@"writeok");

 [myHandle seekToEndOfFile];
 [myHandle writeData:dataZ];
 NSLog(@"writeok");

 [myHandle seekToEndOfFile];

 NSLog(@"zp26 %@",filePath);


}
+3  A: 

You can append line breaks in your formatting. For example:

NSString *tagX = [NSString stringWithFormat:@"<x>%f<x>\n", name];
Laurent Etiemble