1

I have a very simple xml file by name options.xml

<Dat>
    <Name>Tom</Name>
    <Option>1</Option>
</Dat>

Using NSXML I am trying to change "Tom" to "Jim" and save the file. How can I do that. I read many document and there is no straight forward solution. Can some one help me with the code ?

update: I ended up in trying with Gdatasxml

 -(void)saveToXML
{
NSString* path = [[NSBundle mainBundle] pathForResource:@"options" ofType:@"xml"];

NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:path];
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];

GDataXMLElement *rootElement = [GDataXMLElement elementWithName:@"Dat"];

NSArray *mySettings = [doc.rootElement elementsForName:@"Dat"];

for (GDataXMLElement *mySet in mySettings)
{
    NSString *name;
    NSArray *names = [mySet elementsForName:@"Name"];
    if (names.count > 0)
    {
        GDataXMLElement *childElement = (GDataXMLElement *) [names objectAtIndex:0];
        name = childElement.stringValue;
        NSLog(childElement.stringValue);
        [childElement setStringValue:@"Jim"];
    } 
}

[xmlData writeToFile:path atomically:YES];


}

But this is not saving the data. Help.

user1771823
  • 139
  • 1
  • 13
  • 1
    Putz1103 is right, that you really want to parse it into some structure, change the value, and then write the XML back. If you wanted to cheat, you could just load this into a `NSString` and then do a simple `newString = [oldString stringByReplacingOccurrencesOfString:@"Tom" withString:@"Jim"];`. If you need more complicated string searching and replacing, you could use a `NSRegularExpression` or a `NSScanner`. But the robust solution (e.g. the above might not work if you had CDATA elements) is to parse the XML, update your model, and write it back. – Rob Jun 20 '13 at 03:27
  • Did you notice that your code reads in a file, initializes a new object using that file, manipulates the new object how you want to, then writes the original file data back to a new file? – Putz1103 Jun 21 '13 at 15:48

3 Answers3

2

Editing XML is a little difficult in iOS. You need to parse the original xml to a model and then form the xml.

You can make use of 3rd party library such as GDataXML for forming XML from a data source.

//Edited user info saved in a dictionary
NSDictionary *dictionary = @{@"Name": @"Jim", @"Option":@"1"};

GDataXMLElement *rootElement = [GDataXMLElement elementWithName:@"Dat"];
[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    GDataXMLElement *element = [GDataXMLElement elementWithName:key stringValue:obj];
    [rootElement addChild:element];
}];
//xml document is formed
GDataXMLDocument *document = [[GDataXMLDocument alloc]
                              initWithRootElement:rootElement];
NSData *xmlData = document.XMLData;

NSString *filePath = [self savedXMLPath];
//XML Data is written back to a filePath
[xmlData writeToFile:filePath atomically:YES];
Anupdas
  • 10,211
  • 2
  • 35
  • 60
  • I am trying to avoid third party lib to keep my app light. If NSXML doesnt work, I will for sure try GdataXML. Thanks for the help – user1771823 Jun 20 '13 at 16:18
  • refer http://stackoverflow.com/questions/17235752/gdataxml-updating-xml-data to see how i used gdaxml – user1771823 Jun 26 '13 at 02:15
1

Create a class that is essentially an XML "node". Then in your parser setup a system of these XML nodes in the same fashion as you read them. Then search through that body and find the element that you would like to change. Change it. Then write a function that goes through these "node" objects and writes a new NSString in XML format and save that string to file. There is no real easy way that I know of to write XML files. I'm sure someone has a library out there to do it, but I had very complex XML's to deal with so I wrote my own. If you would like specific code let me know and I can try to give you parts of what you may need.

Putz1103
  • 6,211
  • 1
  • 18
  • 25
1

You Can use GDATAXML for changing XML node Here is Working Code snippet

NSString *XMLString = @"<Dat><Name>Tom</Name><Option>1</Option></Dat>";
NSError *error = nil;
GDataXMLElement *newElement = [[GDataXMLElement alloc] initWithXMLString: XMLString error: &error];
NSLog(@"New element: %@ error: %@", newElement, error);
if(nil == error)
{
    GDataXMLElement *childElement = [[newElement elementsForName: @"Name"] objectAtIndex: 0];
    [childElement setStringValue:@"Jim"];

    childElement = [[newElement elementsForName: @"Option"] objectAtIndex: 0];

    [childElement setStringValue:@"2"];

}
NSLog(@"New element now: %@", newElement);

Check by using this code snippet

ani
  • 245
  • 5
  • 21