iOS 给xml文件添加子节点

获取xml文件

NSString *xmlPath = [NSString stringWithFormat:@"%@/EmotionConfig.xml",lastGroupPath];

    NSString *rootPath = [[NSString alloc]initWithString:xmlPath];

    NSData *data1 = [[NSData alloc]initWithContentsOfFile:rootPath];

    GDataXMLDocument *doc1 = [[GDataXMLDocument alloc] initWithData:data1 options:0 error:nil];

    GDataXMLElement *ele1 = doc1.rootElement;

生成子节点

    GDataXMLElement *element = [GDataXMLNode elementWithName:@"Item"];

GDataXMLElement *ShortCut = [GDataXMLNode elementWithName:@"ShortCut" stringValue:@""];

    GDataXMLElement *Meaning = [GDataXMLNode elementWithName:@"Meaning" stringValue:[NSString stringWithFormat:@"%ld",arr.count - 1]];

    GDataXMLElement *OriginalFile = [GDataXMLNode elementWithName:@"OriginalFile" stringValue:[NSString stringWithFormat:@"%ld.png",arr.count - 1]];

    GDataXMLElement *FixedFile = [GDataXMLNode elementWithName:@"FixedFile" stringValue:[NSString stringWithFormat:@"%ld.png",arr.count - 1]];

    GDataXMLElement *GroupName = [GDataXMLNode elementWithName:@"GroupName" stringValue:emoGroupName];

    GDataXMLElement *IsSys = [GDataXMLNode elementWithName:@"IsSys" stringValue:@"0"];

    [element addChild:ShortCut];

    [element addChild:Meaning];

    [element addChild:OriginalFile];

    [element addChild:FixedFile];

    [element addChild:GroupName];

    [element addChild:IsSys];

子节点加入xml文件

    [ele1 addChild:element];

重新写入沙盒

    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithRootElement:ele1];

    NSData *xmlData = [doc XMLData];

    NSString *xmlpathName = @"EmotionConfig.xml";

    NSString *filePath = [lastGroupPath stringByAppendingPathComponent:xmlpathName];

    [xmlData writeToFile:filePath atomically:YES];

你可能感兴趣的:(iOS 给xml文件添加子节点)