简体   繁体   English

XML Tag xcode保存文件

[英]XML Tag xcode save file

I would like to output tags for each line with different tag names and want to save each in a file. 我想为具有不同标签名称的每一行输出标签,并将每个标签保存在文件中。 I can write the file, but it is over writing. 我可以写文件,但是覆盖了。 How do I write each and every line in a different file? 如何将每一行写在不同的文件中?

Input: 输入:

english titanic 泰坦尼克号
leondradicapro kate 伦达卡普

German Hotel hüßßan 德国酒店hüßßan
tomhanks angleina 汤姆汉克斯·安吉利娜

Output: 输出:

<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++;

now i can able to read only the language and movie (1st line) how i can add second line and tag also 现在我只能阅读语言和电影(第一行),如何添加第二行和标签

you need write file with c method, maybe the following method can help you 您需要使用c方法写入文件,也许以下方法可以帮助您

-(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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM