无法将内容添加到XML文件?

时间:2011-06-09 04:14:09

标签: iphone xml

我想将内容添加到XML文件中,我可以使用此代码从XML读取数据

-(NSString *)readfromxml:(NSString *)filename{
NSString *filepath=[[NSBundle mainbundle] pathForResource:filename ofType:@"xml"];
NSData *data=[NSData dataWithContentsOfFile:filepath];
NSString *str,*docstr;
if(data){
str=[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding error:NULL];
}
NSArray *path=NSSearchPathForDirectoriesInDomain[NSDocumentDirectory,NSUserDomainMask,YES];
NSString *documentdirectory=[path objectAtIndex:0];
NSString *appath=[documentdirectory stringByAppendingPathComponent:filename];
NSError *error=null;
BOOl fileexists=[[NSFileManager defaultManager] fileExistsAtPath:path];
if(fileexists)
{
NSLog(@"File Exists in Document");
NSData *mydata=[NSData dataWithContentsOfFile:path];
if(mydata){
docstr=[[NSString alloc] initWithData:mydata encoding:NSASCIIStringEncoding];
}
}
else{
NSLog(@"File Not exists");
BOOL Success=[str writeToFile:apppath automatically:YES  encoding:NSTUF8StringEncoding error:error];
if(!Success)
{
NSLog(@"Error::%@",[error userInfo]);
}
else{
NSLog(@"Write successfuly");
}
NSData *mydata=[NSData dataWithContentsOfFile:path];
if(mydata)
{
docstr=[[NSString alloc] initWithData:mydata encoding:NSASCIIStringEncoding];
}
}
return docstr;
}  

我可以使用此代码读取XML文件,但我无法写入文件。

1 个答案:

答案 0 :(得分:0)