美文网首页
iOS 给xml文件添加子节点

iOS 给xml文件添加子节点

作者: 微笑不是你 | 来源:发表于2018-01-20 09:54 被阅读0次

    获取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文件添加子节点

          本文链接:https://www.haomeiwen.com/subject/pbwzoxtx.html