Вопрос

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.

Это было полезно?

Решение

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];

Другие советы

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.

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top