0
我想將內容添加到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文件讀取XML數據,但我不能寫入文件。