【问题标题】:Modify attribute of xml tag in objective c修改目标c中xml标签的属性
【发布时间】:2012-12-12 04:56:59
【问题描述】:

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

mynode 属性1=""属性2=""属性3="" /我的节点

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

【问题讨论】:

  • 贴一些代码并解释失败的地方
  • GDataXMLElement *node=[GDataXMLNode elementwithName:@"mynode"]; GDataXMLElement *attribute=[GDataXMLNode attributewithName:@"attribute3 stringValue:@"Yes"]; [node addAttribute : attribute]; 在此之后,我正在使用 writeToFile 自动编写 xmlfile。

标签: objective-c xml


【解决方案1】:

无法在 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];

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

【讨论】:

  • 谢谢。我试试看
  • 您可以试试这个,如果您遇到任何问题,请告诉我。
猜你喜欢
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
相关资源
最近更新 更多