开发者

how to append tags in xml in iphone?

开发者 https://www.devze.com 2023-03-13 20:19 出处:网络
i am new developer in iphone application. i would like write a content in xml file for that, i have created xml file and write tags with element, attribute and value with some data in that xml as foll

i am new developer in iphone application. i would like write a content in xml file for that, i have created xml file and write tags with element, attribute and value with some data in that xml as follows.

-(void)writexmlfile:(NSString *)data toFile:(NSString *)fileName NodeName:(NSString *)Node{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

// the path to write file
NSString *fileExtension = [NSString stringWithFormat:@"%@%@",fileName,@".xml"];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileExtension];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:appFile];
if(fileExists) {
    NSError *error = nil;
    NSString *docStr;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    // the path to write file
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:appFile];
    if(fileExists)
    {
        NSLog(@"file exist in document");
        NSData *myData1 = [NSData dataWithContentsOfFile:appFile];
        if (myData1) {  
            docStr = [[NSString alloc] initWithData:myData1 encoding:NSASCIIStringEncoding];
            XMLString = [[NSMutableString alloc]initWithString:docStr];
            [self XMLString];
            NSLog(@"data%@",XMLString);
        } 
    }

    // [XMLString insertString:data atIndex:index];
     [XMLString appendFormat:@"<%@>%@</%@> \n",Node,data,Node];

    BOOL success = [XMLString  writeToFile:appFile  atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (!success) {
        NSLog(@"Error: %@", [error userInfo]);
    }
    else {
        NSLog(@"File write is successful");
    }
}
}

i want add new tag with new elements,new attributes,etc.,if i enter element at the place of tag it is modifying with previous开发者_JS百科 tag .

here how can i append the new tag to previous appended tags

please any body could help me

Thanks in advance


As a suggestion, you can use APXML to create XML documents it is very simple and easy to use. This post can guide you further.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号