修改目标c中xml标记的属性

时间:2012-12-12 04:56:59

标签: objective-c xml

我想在某些操作上使用objective c修改xml属性 例如,我在屏幕上有按钮,我的xml标签是

mynode attribute1 =“”attribute2 =“”attribute3 =“” / MYNODE

如果attribute3的初始值为NO,则在点击按钮上我想将其更改为YES并将其写入项目目录中的xml文件。 如果有人知道这个,请帮助我。 我尝试过使用GDataXML addattribute方法,但无法修改标记。

1 个答案:

答案 0 :(得分:2)

无法在Objective-c中编辑XML文件。如果要执行此操作,则需要创建自己的xml文件,或者可以将标记附加到现有xml。 请参考以下代码,我创建了自己的xml,以附加图像的字节数组&将其添加到原始xml。

//Encode all the data and get the XML string 
            NSString *theXML = [[NSString alloc] 
                                initWithBytes: [clipSvgData bytes] 
                                length:[clipSvgData length] 
                                encoding:NSUTF8StringEncoding];

            //Returns the substring of an main xml file by excluding the extra xml tags
            NSString *startTag = @"<svg";
            NSString *endTag = @"</svg>";
            NSString *responseString;

            NSScanner *scanner = [[NSScanner alloc] initWithString:theXML];
            [scanner scanUpToString:startTag intoString:nil];
            scanner.scanLocation += [startTag length];
            [scanner scanUpToString:endTag intoString:&responseString];
            [scanner release];

            //Remove the SVG tag from main xml
            NSString *startTag1 = @">";
            NSString *endTag1 = @"</svg>";
            NSString *responseString1;

            NSScanner *scanner1 = [[NSScanner alloc] initWithString:[NSString stringWithFormat:@"<svg %@ </svg>",responseString]];
            [scanner1 scanUpToString:startTag1 intoString:nil];
            scanner1.scanLocation += [startTag1 length];
            [scanner1 scanUpToString:endTag1 intoString:&responseString1];
            [scanner1 release];

    NSString *strSVGNode = [NSString stringWithFormat:@"<svg version=\"1.1\" id=\"Layer_1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" x=\"%fpx\" y=\"%fpx\" width=\"%fpx\" height=\"%fpx\" viewBox=\"0 0 141.73 141.73\" enable-background=\"new 0 0 141.73 141.73\"  xml:space=\"preserve\" preserveAspectRatio=\"none\">", imgXPos, imgYPos, imgWidth, imgHeight];

    NSString *strClipXml = [NSString stringWithFormat:@"%@ %@ </svg></g>",strSVGNode ,responseString1];

    //Add the created SVG as new node in main SVG file.
    GDataXMLNode *clipNode = [GDataXMLNode textWithStringValue:[NSString stringWithFormat:@"%@",strClipXml]];
    [xmlDocument.rootElement addChild:clipNode];

您可以根据自己的要求进行修改。

相关问题