写入xml iOS

-(void)updateXML
{
    GDataXMLElement *notesElements=[GDataXMLNode elementWithName:@"picNotes"];
    for (int i=0; i<[picTodoArray count]; i++) {
        NSLog(@"here");
        GDataXMLElement *noteElement=[GDataXMLNode elementWithName:@"picNote"];
        GDataXMLElement *imageIDElement=[GDataXMLNode elementWithName:@"imageID" stringValue:[NSString stringWithFormat:@"%d",[[picTodoArray objectAtIndex:i] imageID]]];
        GDataXMLElement *pictureNameElement=[GDataXMLNode elementWithName:@"pictureName" stringValue:[[picTodoArray objectAtIndex:i] pictureName]];
        GDataXMLElement *descriptionElement=[GDataXMLNode elementWithName:@"description" stringValue:[[picTodoArray objectAtIndex:i] todoDescription]];
        NSString *filePath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSLog(@"%@",filePath);
        filePath=[filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"imgData%d.png",[picTodoArray count]-1]];
        [[[picTodoArray objectAtIndex:i] imageData] writeToFile:filePath atomically:YES];
        GDataXMLElement *imageDataElement=[GDataXMLNode elementWithName:@"imageData" stringValue:filePath];
        [noteElement addChild:imageIDElement];
        [noteElement addChild:pictureNameElement];
        [noteElement addChild:descriptionElement];
        [noteElement addChild:imageDataElement];
        [notesElements addChild:noteElement];
    }
    GDataXMLDocument *doc=[[GDataXMLDocument alloc]initWithRootElement:notesElements];
    [doc setCharacterEncoding:@"utf-8"];
    NSData *xmlData=doc.XMLData;
    NSString *xmlfilePath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    xmlfilePath=[xmlfilePath stringByAppendingPathComponent:@"picData.xml"];
    [xmlData writeToFile:xmlfilePath atomically:YES];
}

你可能感兴趣的:(写入xml iOS)