开发者

unable to add content to XML file?

I want to add content to XML file, I can read data from XML using this code

-(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开发者_StackOverflow社区 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;
}  

I can read XML file using this code, but I can't write file.


http://iphone-bitcode.blogspot.com/2011/06/gdataxml-is-googles-xml-processing.html it will help you...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜