【问题标题】:Parsing XML in Cocoa在 Cocoa 中解析 XML
【发布时间】:2010-09-16 22:52:22
【问题描述】:

我有这个已导入 Cocoa 的 XML 文件,并想修改一个值。我怎么做 ?好像没有什么好的例子。 我将此文件导入 Cocoa(正常工作),现在想更改 imp 的值,然后再将其传输到服务器。我该怎么做呢?谢谢

  <?xml version='1.0'?>
  <Root xmlns='http://www.abc.uk' version='1.0' name='full'>
    <child1 version='2.0'>
     <value1>
       <user>abc</user>
       <pass>xyz</pass>
     </value1>
    </child1>
    <child2>
     <imp>12345</imp>
    </child2>
 </Root>

我有这个解决方案,但它没有按我希望的那样工作。

            xmlDoc = [[NSXMLDocument alloc] initWithContentsOfURL:furl     
            options:NSXMLDocumentTidyXML error:&err];

            NSArray *children = [[xmlDoc rootElement] children];
    int i, count = [children count];

    //loop through each child
     for (i=0; i < count; i++) 
        {
        NSXMLElement *child = [children objectAtIndex:i];
            NSLog(@"child Name is %@", child.name);

            if ([child.name isEqual:@"child2"]) 
                {
    [[child attributeForName:@"imp"] setStringValue:@"4567"];
    break;

                }       
        }   

【问题讨论】:

  • NSLog(@"值为 %@",[child childAtIndex:0]);让我得到 imp 参数的值。仍在寻找访问和更改它的方法。

标签: xml cocoa


【解决方案1】:

这样做:

NSXMLNode *new = [NSXMLNode elementWithName:@"imp" stringValue:@"12345"];
[child replaceChildAtIndex:0 withNode:new];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    相关资源
    最近更新 更多