2013-01-16 5 views
0

Je voudrais sortir des étiquettes pour chaque ligne avec différents noms de balises et je veux les enregistrer dans un fichier. Je peux écrire le fichier, mais c'est fini. Comment écrire chaque ligne dans un fichier différent?XML Tag xcode sauvegarder le fichier

Entrée:

anglais titanesque
leondradicapro kate

Allemand Hotel hüßßan
tomhanks angleina

Sortie:

<language>english<language/> 
<movie>titanic<movie/> 
<Actor>leondradiacpro<Actor/> 
<Actress>kate<Actress/> 

<language>German<language/> 
... 

code:

for (NSString *str in lineFile) 

       { 

        if([str hasPrefix:@"english "]) 
        { 

         NSMutableArray *dd = [[NSMutableArray alloc] initWithArray:[[str stringByReplacingOccurrencesOfString:@"english" withString:@""] componentsSeparatedByString:@" "]]; 

        NSArray *tag = [NSArray aarrayWithObjects:@"er",@"langauge",@"movie",@"actor",@"kate",nil]; 

        NSMutableString *output =[[NSMutableString alloc] init]; 
        NSUInteger tagindex =0; 

        for (NSString *words in word) 

        { 
         if(tagindex >=[tag count]) 
         { 

          NSLog(@"dad"); 
          break; 
        } 
         [output appendFormat:@"<%@>%@<%@>\n", [tag objectAtIndex:tagindex],words,[tag objectAtIndex:tagindex]]; 
         NSLog(@"%@",output); 
[output writeToFile:@"/Users/xx/Desktop/homework.txt" atomically:YES encoding:NSASCIIStringEncoding error:NULL];  
         tagindex++; 

maintenant je peux en mesure de lire que la langue et le film (1ère ligne) comment je peux ajouter deuxième ligne et l'étiquette aussi

Répondre

0

vous devez écrire le fichier avec la méthode c, peut-être les suivantes méthode peut vous aider

-(void)writeToFile:(NSString *)str:(NSString *) fileName 
{ 
    NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding]; 
    NSString *filePath=[NSString stringWithFormat:@"%@",fileName]; 
    if([[NSFileManager defaultManager] fileExistsAtPath:filePath] == NO){ 
     NSLog(@"file not exist,create it..."); 
     [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil]; 
    }else { 
       NSLog(@"file exist!!!"); 
    } 

    FILE *file = fopen([filePath UTF8String], [@"ab+" UTF8String]); 

    if(file != NULL){ 
     fseek(file, 0, SEEK_END); 
    } 
    int readSize = [data length]; 
    fwrite((const void *)[data bytes], readSize, 1, file); 
    fclose(file); 
}