我有从XML文件创建的GDataXMLDocument。
正在从文件中读取的XML:
<Pages Count="1"> <Page PageNumber="1"></Page> </Pages>
我需要添加新的"Page“标签(以编程方式)。在添加新的"Page“后添加XML:
<Pages Count="2"> <Page PageNumber="1"></Page> <Page PageNumber="2"></Page> </Pages>
只要不需要将stringValue设置为“页面”标签,一切都是可以的;
XML应该如下所示:
<Pages Count="2"> <Page PageNumber="1">Some value</Page> <Page PageNumber="2">Some other value</Page> </Pages>
在程序中它看起来!(您可以使用XMLString方法看到这一点),但是当我将文档(文档XMLData)保存回文件时,它看起来如下所示:
<Pages Count="2"> <Page PageNumber="1">Some value</Page> <Page PageNumber="2"></Page> </Pages>
编号为1的页面(旧页面)有值,但编号为2的页面(新页面)没有值。为什么我可以在旧标签上更改stringValue,但不能在新标签上更改?
发布于 2014-03-21 21:34:37
这对我来说很有用,
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]);https://stackoverflow.com/questions/22558048
复制相似问题