0

I have GDataXMLDocument created from XML file.

XML being read from file:

<Pages Count="1"> <Page PageNumber="1"></Page> </Pages>

I need to add new "Page" tag (programmatically). XML after new "Page" added:

<Pages Count="2"> <Page PageNumber="1"></Page> <Page PageNumber="2"></Page> </Pages>

Everything is OK as long as there is no need to set stringValue to "Page" tags;

XML should look like this:

<Pages Count="2"> <Page PageNumber="1">Some value</Page> <Page PageNumber="2">Some other value</Page> </Pages>

and in program it looks! (you can see this with XMLString method), but when I save the document ([document XMLData]) back to file it looks like this:

<Pages Count="2"> <Page PageNumber="1">Some value</Page> <Page PageNumber="2"></Page> </Pages>

Page with number 1 (the old one) has value, but Page with number 2 (the new one) has not. Why can I change stringValue on old tags, but can't on the new ones?

Povilas
  • 445
  • 3
  • 17

1 Answers1

0

Its work for me,

GDataXMLElement *element = [GDataXMLElement elementWithName:@"Pages" stringValue:nil];
[element addAttribute:[GDataXMLNode attributeWithName:@"Count" stringValue:@"1"]];


GDataXMLElement *tempNode = [GDataXMLNode elementWithName:@"Page" stringValue:@"Some Value"];
[tempNode addAttribute:[GDataXMLNode attributeWithName:@"PageNumber" stringValue:@"1"]];

[element addChild:tempNode];

tempNode = [GDataXMLNode elementWithName:@"Page" stringValue:@"Some Other Value"];
[tempNode addAttribute:[GDataXMLNode attributeWithName:@"PageNumber" stringValue:@"2"]];

[element addChild:tempNode];

[element attributeForName:@"Count"].stringValue = @"2";

GDataXMLDocument *myDoc = [[GDataXMLDocument alloc] initWithRootElement:element];

NSData *data = [myDoc XMLData];

NSArray *dirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [dirPath objectAtIndex:0];
path = [path stringByAppendingString:@"/myXml.xml"];

// log path file
NSLog(@"%@", path);

[data writeToFile:path atomically:YES];

data = nil;

data = [NSData dataWithContentsOfFile: path];

myDoc = nil;

myDoc = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];

NSLog(@"%@", [myDoc rootElement]);
Khalil
  • 238
  • 2
  • 15
  • Well, this works for me either. But you should add second page AFTER GDataXMLDocument initialization and then try to change string value and save file. – Povilas Mar 21 '14 at 14:11