tags:

views:

40

answers:

1

I have this XML file that I have imported into Cocoa and want to modify one value. How do I do that ? There don't seem to be any good examples. I imported this file into Cocoa ( works properly )and now want to change the value of imp, before I transmit it to a server. How would I go about doing this ? Thanks

  <?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>

I have this solution, but its not working as I would want it to.

            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;

                }       
        }   
+3  A: 

This does it:

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